6

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
9
  • 2
    Isn't this a method call requiring a parameter, not an assignment. So instead of x = y, its x(y): msdn.microsoft.com/en-us/library/office/ff869553.aspx
    – majjam
    Nov 11, 2013 at 16:53
  • @bamie9l Awesome! That worked, but now at the .BodyFormat = olFormatHTML line I get Run-time error '5': Invalid procedure call or argument
    – Adjit
    Nov 11, 2013 at 16:56
  • 1
    Declare this at the top of your code Const olFormatHTML = 2 or replace .BodyFormat = olFormatHTML by .BodyFormat = 2 Nov 11, 2013 at 16:59
  • @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
    – Adjit
    Nov 11, 2013 at 17:04
  • Debug that line and see what is the value of subject at the time of error? Nov 11, 2013 at 17:06

1 Answer 1

7

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
1
  • Spot on! Thanks so much! can't believe I didn't think of that before. Now that you say that, it makes complete sense. In essence I was trying to send a sent email (an email that no longer exists) over and over again...
    – Adjit
    Nov 11, 2013 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.