Send Multiple attachments to email

M

Mark

HI having problem with sending more than one attachment
Using the code below that I got off this site
Works fine sending one attachment but I need to add 2
Any ideas please?

-------------------------------------------------------------------------------------
Sub sbSendMessage(Optional attachmentpath)
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim objOutlookAttach As Outlook.Attachment

Dim Subjectdate
Subjectdate = Format(Date, "Long date")


' On Error GoTo ErrorMsgs

' Create the Outlook session.
Set objOutlook = CreateObject("Outlook.Application")
' Create the message.
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
With objOutlookMsg
' Add the To recipient(s) to the message. Substitute
' your names here.
Set objOutlookRecip = .Recipients.Add("Mark")
objOutlookRecip.Type = olTo
' Add the CC recipient(s) to the message.
Set objOutlookRecip = .Recipients.Add("Mark")
objOutlookRecip.Type = olCC
' Set the Subject, Body, and Importance of the message.
.Subject = Subjectdate
.Body = "" & vbCrLf & vbCrLf

' Add attachments to the message.
If Not IsMissing(attachmentpath) Then
Set objOutlookAttach = .Attachments.Add(attachmentpath)

End If


' Resolve each Recipient's name.
For Each objOutlookRecip In .Recipients
If Not objOutlookRecip.Resolve Then
objOutlookMsg.Display
'End If
End If
Next
.Send
End With
Set objOutlookMsg = Nothing
Set objOutlook = Nothing
Set objOutlookRecip = Nothing
Set objOutlookAttach = Nothing
'ErrorMsgs:
' If Err.Number = "287" Then
' MsgBox "You clicked No to the Outlook security warning. " & "Rerun
the procedure and click Yes to access e-mail" & "addresses to send your
message. For more information," & "see the document at
http://www.microsoft.com/office" & "/previous/outlook/downloads/security.asp.
"
' Else
' MsgBox Err.Number, Err.Description
' End If
End Sub
 
S

Stuart McCall

Mark said:
HI having problem with sending more than one attachment
Using the code below that I got off this site
Works fine sending one attachment but I need to add 2
Any ideas please?

-------------------------------------------------------------------------------------
Sub sbSendMessage(Optional attachmentpath)
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim objOutlookAttach As Outlook.Attachment

Dim Subjectdate
Subjectdate = Format(Date, "Long date")


' On Error GoTo ErrorMsgs

' Create the Outlook session.
Set objOutlook = CreateObject("Outlook.Application")
' Create the message.
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
With objOutlookMsg
' Add the To recipient(s) to the message. Substitute
' your names here.
Set objOutlookRecip = .Recipients.Add("Mark")
objOutlookRecip.Type = olTo
' Add the CC recipient(s) to the message.
Set objOutlookRecip = .Recipients.Add("Mark")
objOutlookRecip.Type = olCC
' Set the Subject, Body, and Importance of the message.
.Subject = Subjectdate
.Body = "" & vbCrLf & vbCrLf

' Add attachments to the message.
If Not IsMissing(attachmentpath) Then
Set objOutlookAttach = .Attachments.Add(attachmentpath)

End If


' Resolve each Recipient's name.
For Each objOutlookRecip In .Recipients
If Not objOutlookRecip.Resolve Then
objOutlookMsg.Display
'End If
End If
Next
.Send
End With
Set objOutlookMsg = Nothing
Set objOutlook = Nothing
Set objOutlookRecip = Nothing
Set objOutlookAttach = Nothing
'ErrorMsgs:
' If Err.Number = "287" Then
' MsgBox "You clicked No to the Outlook security warning. " & "Rerun
the procedure and click Yes to access e-mail" & "addresses to send your
message. For more information," & "see the document at
http://www.microsoft.com/office" &
"/previous/outlook/downloads/security.asp.
"
' Else
' MsgBox Err.Number, Err.Description
' End If
End Sub

Well you could make the procedure accept 2 parameters:

Sub sbSendMessage(Optional attachment1path, Optional attachment2path)

then add them like:

If Not IsMissing(attachment1path) Then
Set objOutlookAttach = .Attachments.Add(attachment1path)
End If

If Not IsMissing(attachment2path) Then
Set objOutlookAttach = .Attachments.Add(attachment2path)
End If

but a more forward-thinking way would be to use a ParamArray, which allows
you to pass variable numbers of attachments (in case someone decides you now
need to send 3 attachments) :

Sub sbSendMessage(Optional ParamArray attachmentpaths())

then add them like:

Dim item As Variant

For Each item In attachmentpaths
Set objOutlookAttach = .Attachments.Add(item)
Next
 
M

Mark

thanks Stuart, you are a genius
i used the first option and it works well enough for what i need
the 2nd option i kept getting an red error on ParamArray

M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top