Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA发送带有多个附件的电子邮件_Vba_Excel - Fatal编程技术网

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

Excel VBA发送带有多个附件的电子邮件,vba,excel,Vba,Excel,所以我们正在举办这个大型活动,我有一张excel表格,上面有每个人的姓名、电子邮件地址以及他们的行程文件(其中有两个)单元格(x,3)和单元格(x,4)。我想做的是沿着专栏往下走,给每个人发一封“个性化”的电子邮件,里面有他们所有的信息 在代码中,for的循环只转到3,因为我只是通过向自己发送电子邮件来测试它,不想最终收到1000封电子邮件:p 在我尝试添加附件的行中,我不断收到运行时错误440(自动化错误)。。。不知道发生了什么事或如何补救。感谢您的帮助 代码 Sub CreateHTMLMa

所以我们正在举办这个大型活动,我有一张excel表格,上面有每个人的姓名、电子邮件地址以及他们的行程文件(其中有两个)
单元格(x,3)
单元格(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
Sub-CreateHTMLMail()
'创建新的电子邮件项目并修改其属性。
作为对象的Dim-olApp
Dim objMail作为对象
将正文、标题、文件路径、主题设置为字符串
暗x等长
设置olApp=CreateObject(“Outlook.Application”)
'创建电子邮件项目
Set objMail=olApp.CreateItem(0)
filePath=“\\fileserver\homeshares\Tsee\My Documents\Metropolitan Sales\MNF”
主题=“本周末MNF活动的重要旅行信息”
x=1
对于x=1到3
head=“

Hi”和单元格(x,1)。值&“,

” body=body&“

我们期待着您参加我们的大都会夜间足球赛事这个即将到来的周日,11/17!请注意,巨人队的比赛时间已从晚上8:30改为下午4:25。

” body=body&“

请查看随附的包含重要地址和确认号码的旅行信息包。请仔细阅读,如果您有任何问题,请告诉我。

” body=body&“

如果你这个周末需要联系我,请打我的手机(631)793-9047或发电子邮件给我。

” body=body&“

谢谢,
Liz

” 用objMail .主语 .To=单元格(x,2).值 .Attachments.Add=filePath&“/”单元格(x,3).Value .Attachments.Add=filePath&“/”单元格(x,4).Value .BodyFormat=olFormatHTML .HTMLBody=头部和身体 .发送 以 下一个x 端接头
除了上述评论,@bamie9l已经解决了您的一个问题

问题2


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

您正在使用Excel中的Outlook进行后期绑定,
olFormatHTML
是Outlook常量,因此Excel无法识别它。在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
x=1到3的

Set objMail=olApp.CreateItem(0)
head=“

Hi”和单元格(x,1)。值&“,

” Body=“诸如此类” 用objMail .主语 .To=单元格(x,2).值 .Attachments.Add=FilePath&“/”单元格(x,3).Value .Attachments.Add=FilePath&“/”单元格(x,4).Value .BodyFormat=olFormatHTML .HTMLBody=头部和身体 .发送 以 下一个x
这不是一个需要参数而不是赋值的方法调用吗。所以不是x=y,而是x(y):@bamie9l太棒了!这是可行的,但现在在
.BodyFormat=olFormatHTML
行,我得到了运行时错误“5”:无效的过程调用或参数在代码顶部声明此错误
Const olFormatHTML=2
或将
.BodyFormat=olFormatHTML
替换为
.BodyFormat=2
@SiddharthRout,这样就行了,但现在我遇到了一个疯狂的自动化错误。。。它只经过一次循环。。发送1封电子邮件,然后当它到达
。subject=subject
时,我得到运行时错误“-2147221238(8004010a)”:自动化错误,据我所知,这与运行时错误440相同。调试该行并查看出错时
subject
的值是多少?马上!非常感谢!真不敢相信我以前没想到。既然你这么说,那就完全有道理了。本质上,我是想一次又一次地发送一封已发送的电子邮件(一封不再存在的电子邮件)。。。