Send multiple attachments using outlook (1 Viewer)

Ronaldo9

Registered User.
Local time
Today, 11:59
Joined
Jul 20, 2011
Messages
21
I would really appreciate some help here...

I'm trying to use the SendEmails function below to automatically send email to users with multiple attachments.
When I call the function as such, it works fine as long as I have one attachment.
Code:
 SendEmails(strTo, strSubject, strMessage, arrAttach)
However, users need to send more than one attachment(max 3) sometime . The attachments are populated in the 3 fields on a form with the attachment path in them.
the first attachment field is mandatory, the other two, may or not be populated but they usually are.
After the user select the attachments, I need to call the SendEmails function and pass the attachments with the arrAttach arguments.

Any idea how this could be done ???. I tried different methods but was not successful.
the SendEmails function is below. It's declared as a Module.



Code:
Public Function SendEmails(strTo As String, _
                     strSubject As String, _
                     strMessage As String, _
                      strAttachment As String)
On Error GoTo ErrHandle_Err

Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim objOutlookAttach As Outlook.Attachment

'Set the variables
Set objOutlook = CreateObject("Outlook.Application")
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)

          With objOutlookMsg
                objOutlookMsg.To = strTo
                objOutlookMsg.Subject = strSubject
                objOutlookMsg.HTMLBody = strMessage
                objOutlookMsg.BodyFormat = olFormatHTML
                objOutlookMsg.Importance = olImportanceHigh
                objOutlookMsg.Attachments.Add strAttachment, olByRef, 1, "Document"

objOutlookMsg.Send
          End With
          Set objOutlook = Nothing
ErrHandle_Exit:

    Exit Function

ErrHandle_Err:
Exit Function

End Function
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 14:59
Joined
Sep 12, 2006
Messages
15,613
this line

objOutlookMsg.Attachments.Add strAttachment, olByRef, 1, "Document"


the red number is the index of the attachment. you need to change it for each successive attachment

note that the 4th argument "document" only works for certain file types. generally any attachment will be shown on the email as "strattachment" rather than "document". This is a documented "feature".
 

Ari

Registered User.
Local time
Today, 07:59
Joined
Oct 22, 2011
Messages
139
Hi Ronaldo

See if the changes will assist you.

Code:
Public Function SendEmails(strTo As String, _
                     strSubject As String, _
                     strMessage As String, _
                     Optional strAttachment1 As String, _
                     Optional strAttachment2 As String, _
                     Optional strAttachment3 As String)
On Error GoTo ErrHandle_Err
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim objOutlookAttach As Outlook.Attachment
Dim j As Byte, strSec As String, k As Variant
'Set the variables
Set objOutlook = CreateObject("Outlook.Application")
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
          With objOutlookMsg
                .To = strTo
                .Subject = strSubject
                .HTMLBody = strMessage
                .BodyFormat = olFormatHTML
                .Importance = olImportanceHigh
 
                strSec = strAttachment1 & "|" & strAttachment2 & "|" & strAttachment3
                k = Split(strSec, "|")
                For j = 0 To UBound(k)
                    If Len(k(j) & "") > 0 Then
                        .Attachments.Add k(j), olByRef, 1, "Document" & (j + 1)
                    End If
                Next
                .send
          End With
          Set objOutlook = Nothing
ErrHandle_Exit:
    Exit Function
ErrHandle_Err:
Exit Function
End Function

Success!
 

Ronaldo9

Registered User.
Local time
Today, 11:59
Joined
Jul 20, 2011
Messages
21
Thank you guys for the replies. They were all helpful.

Ari you made my day.. I used your solution and it works great.:)

Just one more simple question for you.
if I call the the function with 3 atatchments as such:
Code:
SendEmails(strTo, strSubject, strMessage, Me.txtAttach1,Me.txtAttach2,Me.txtAttach3)

If one of the attachment fields is blank, I get the "invalid use of Null" error.
How can I get around this without using if statement for each time i have to call the function. Is there an easy "clean" fix for this.

Thank you in advance..
 

Ari

Registered User.
Local time
Today, 07:59
Joined
Oct 22, 2011
Messages
139
Hi

SendEmails(strTo, strSubject, strMessage, nz(Me.txtAttach1,""),nz(Me.txtAttach2,""),nz(Me.txtAttach3,""))

Success!
 

Ronaldo9

Registered User.
Local time
Today, 11:59
Joined
Jul 20, 2011
Messages
21
Of course !!!.. I forgot about nz...
Thank you very much and God Bless!!
 

Users who are viewing this thread

Top Bottom