5

所以我们正在举办这个大型活动,我有一个 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
4

1 回答 1

6

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

问题 2

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

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

在此处输入图像描述

因此,我们必须在 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
于 2013-11-11T17:15:14.187 回答