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

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

2
这不是需要参数的方法调用吗,而不是赋值。所以应该是 x(y),而不是 x = y。http://msdn.microsoft.com/en-us/library/office/ff869553.aspx - majjam
1
在您的代码顶部声明此内容:Const olFormatHTML = 2 或者将 .BodyFormat = olFormatHTML 替换为 .BodyFormat = 2 - Siddharth Rout
@SiddharthRout 编辑... 抱歉看错了... 我怎么能够查看那个时间的值? - Adjit
你知道如何使用 F8 逐步执行代码吗? - Siddharth Rout
不用等..我知道错误在哪了 :) 正在发布答案... - Siddharth Rout
显示剩余4条评论
1个回答

7

根据以上评论,@bamie9l已经解决了你的一个问题。

问题2

@bamie9l 太棒了!这个方法可以用,但是在 .BodyFormat = olFormatHTML 这一行代码时我遇到了“运行时错误'5':无效的过程调用或参数”- metsales 13分钟前

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

enter image description here

因此,我们必须在Excel中声明该常量。正如我所提到的,要么您可以将 Const olFormatHTML = 2 放置在代码顶部,要么用 .BodyFormat = 2 替换 .BodyFormat = olFormatHTML

问题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

太准确了!非常感谢!我简直不敢相信我以前没有想到过那个。现在你这么说,完全有道理。本质上,我一直在试图发送一封已经不存在的邮件... - Adjit

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接