Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel VBA Sending emails with multiple attachements

Tags:

excel

vba

So we are holding this big event and I have an excel sheet with everyones name, email address as well as their itinerary files (there are 2 of them) Cells(x, 3) and Cells(x, 4). What I am trying to do is go down the column and send everyone a 'personalized' email with all of their information.

In the code, the for loop only goes to 3 because I am just testing it out by sending the emails to myself and don't want to end up getting 1000 emails :P

I keep getting a Run-Time Error 440 (Automation Error) at the lines where I attempt to add the attachments... not sure what's going on or how to remedy it any help is appreciated

Code

Sub CreateHTMLMail()
'Creates a new e-mail item and modifies its properties.

    Dim olApp As Object
    Dim objMail As Object
    Dim body, head, filePath, subject As String
    Dim x As Long
    Set olApp = CreateObject("Outlook.Application")
    'Create e-mail item
    Set objMail = olApp.CreateItem(0)

    filePath = "\\fileserver\homeshares\Tsee\My Documents\Metropolitan Sales\MNF"
    subject = "Important Travel Information for MNF Event this weekend"

    x = 1

    For x = 1 To 3
        head = "<HTML><BODY><P>Hi " & Cells(x, 1).Value & ",</P>"
        body = body & "<BR /><P>We are looking forward to having you at our <STRONG>Metropolitan Night Football Event</STRONG> this upcoming Sunday, <STRONG>11/17</STRONG>!  Note, that the Giants game time has changed from 8:30 PM to 4:25 PM.</P>"
        body = body & "<BR /><P>Please find attached your travel information packet that contains important addresses and confirmation numbers.  Please read through it and let me know if you have any questions.</P>"
        body = body & "<BR /><P>If you need to reach me this weekend, please call my cell phone <STRONG>(631) 793-9047</STRONG> or email me.</P>"
        body = body & "<BR /><P>Thanks,<BR />Liz</P></BODY></HTML>"

        With objMail
            .subject = subject
            .To = Cells(x, 2).Value
            .Attachments.Add = filePath & "/" & Cells(x, 3).Value
            .Attachments.Add = filePath & "/" & Cells(x, 4).Value
            .BodyFormat = olFormatHTML
            .HTMLBody = head & body
            .Send
        End With
    Next x

End Sub
like image 971
Adjit Avatar asked Nov 11 '13 16:11

Adjit


People also ask

How do I send an email with multiple attachments in Excel?

Click + Alias, filters, personalized attachments. Select Attach files in column “...” to emails sent, and click Back. Click Send emails.


1 Answers

Further to the above comments, @bamie9l has already solved one problem of yours

Problem 2

@bamie9l Awesome! That worked, but now at the .BodyFormat = olFormatHTML line I get Run-time error '5': Invalid procedure call or argument – metsales 13 mins ago

You are latebinding with Outlook from Excel and olFormatHTML is an Outlook constant and hence Excel is unable to recognize it. In the Immediate Window of MS-Outlook if you type ?olFormatHTML then you will note that the value of that constant is 2

enter image description here

Hence we have to declare that constant in Excel. Like I mentioned, either you can put Const olFormatHTML = 2 at the top of the code or replace .BodyFormat = olFormatHTML by .BodyFormat = 2

Problem 3

@SiddharthRout So that works, but now I get a crazy automation error... it goes through the loop once.. sends 1 email and then when it gets up to .subject = subject I get Run-time error '-2147221238 (8004010a)': Automation Error which as far as I know is the same as Run-Time Error 440 – metsales

The problem is that you are creating the outlook item outside the loop by

Set objMail = olApp.CreateItem(0)

Outlook already sent that email and now for the next email you will have to re-create it. So move that line inside the loop.

For x = 1 To 3
    Set objMail = olApp.CreateItem(0)

    head = "<HTML><BODY><P>Hi " & Cells(x, 1).Value & ",</P>"
    Body = "Blah Blah"

    With objMail

        .subject = subject
        .To = Cells(x, 2).Value
        .Attachments.Add = FilePath & "/" & Cells(x, 3).Value
        .Attachments.Add = FilePath & "/" & Cells(x, 4).Value
        .BodyFormat = olFormatHTML
        .HTMLBody = head & Body
        .Send
    End With
Next x
like image 182
Siddharth Rout Avatar answered Sep 27 '22 17:09

Siddharth Rout