所以我们正在举办这个大型活动,我有一张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
答案 0 :(得分:4)
除了上述评论之外,@ bamie9l已经解决了你的一个问题
问题2
@ bamie9l太棒了!这工作,但现在在.BodyFormat = olFormatHTML行,我得到运行时错误'5':无效的过程调用或参数 - metsales 13分钟前
您在Excel中使用Outlook进行后期绑定,olFormatHTML
是Outlook常量,因此Excel无法识别它。在Immediate Window
的MS-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