Excel VBA 发送带有多个附件的电子邮件

Adj*_*jit 5 excel vba

所以我们正在举办这个大型活动,我有一个 Excel 表格,上面有每个人的姓名、电子邮件地址以及他们的行程文件(其中有 2 个)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
Run Code Online (Sandbox Code Playgroud)

Sid*_*out 6

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

问题二

@bamie9l 太棒了!那行得通,但现在在 .BodyFormat = olFormatHTML 行,我收到运行时错误 '5': Invalid procedure call or argument – metsales 13 分钟前

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

在此处输入图片说明

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

问题三

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

问题是您正在通过以下方式在循环外创建 Outlook 项目

Set objMail = olApp.CreateItem(0)
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)