Excel VBA发送包含多个附件的电子邮件

时间:2013-11-11 16:50:14

标签: excel vba excel-vba

所以我们正在举办这个大型活动,我有一张excel表,其中包含每个人的姓名,电子邮件地址以及他们的行程文件(其中有两个)Cells(x, 3)Cells(x, 4)。我要做的就是沿着专栏向每个人发送一封包含所有信息的“个性化”电子邮件。

在代码中,for循环只会变为3,因为我只是通过向自己发送电子邮件来测试它,并且最终不想收到1000封电子邮件:P

我在尝试添加附件的行中不断收到运行时错误440(自动化错误) ...不确定发生了什么或如何解决它任何帮助表示赞赏

代码

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

1 个答案:

答案 0 :(得分:4)

除了上述评论之外,@ bamie9l已经解决了你的一个问题

问题2

  

@ bamie9l太棒了!这工作,但现在在.BodyFormat = olFormatHTML行,我得到运行时错误'5':无效的过程调用或参数 - metsales 13分钟前

您在Excel中使用Outlook进行后期绑定,olFormatHTML是Outlook常量,因此Excel无法识别它。在Immediate Window的MS-Outlook中,如果您输入?olFormatHTML,那么您会注意到该常量的值为2

enter image description here

因此我们必须在Excel中声明该常量。就像我提到的那样,您可以将Const olFormatHTML = 2放在代码的顶部,或者将.BodyFormat = olFormatHTML替换为.BodyFormat = 2

问题3

  

@SiddharthRout这样可行,但现在我得到了一个疯狂的自动化错误...它经过循环一次..发送1封电子邮件,然后当它起来.subject = subject我得到运行时错误' - 2147221238(8004010a)':自动化错误据我所知与运行时错误440相同 - metsales

问题是你是在

之外创建循环外的outlook项目
Set objMail = olApp.CreateItem(0)

Outlook已发送该电子邮件,现在您需要重新创建该电子邮件。所以在循环中移动该行。

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