我使用以下代码使用outlook从excel发送电子邮件:
Private Sub SendEmail() Set OutlookApp = CreateObject("Outlook.Application") Set OlObjects = OutlookApp.GetNamespace("MAPI") Set newmsg = OutlookApp.CreateItem(olMailItem) newmsg.Recipients.Add ("name@domain.com; name2@domain.com; name3@domain.com") newmsg.Subject = "Test Mail" newmsg.Body = "This is a test email." 'newmsg.Display newmsg.Send End Sub
代码工作正常,但是当我尝试发送电子邮件时,我从Outlook得到以下错误:
ErrorScreen http://im58.gulfup.com/GRENlB.png
奇怪的是,如果我将新消息打开两三分钟,名称将自动得到解决:
工作http://im74.gulfup.com/qmOYGQ.png
但是这不适合我,因为我不希望在发送之前显示消息.我希望在运行代码后立即发送它.
任何建议或解决方法将不胜感激.
作为旁注:我已经尝试在Outlook中启用"允许逗号作为电子邮件分隔符"选项,然后使用逗号而不是分号,但我仍然面临同样的问题.
更新:
以下是工作代码,根据Dmitry Streblechenko的回答:
Private Sub SendEmail() Dim OutApp As Object Dim OutMail As Object Dim strbody As String Set OutApp = CreateObject("Outlook.Application") Set OlObjects = OutApp.GetNamespace("MAPI") Set OutMail = OutApp.CreateItem(olMailItem) On Error Resume Next With OutMail .To = ("name@domain.com; name2@domain.com; name3@domain.com") .Subject = "Test Mail" .Body = "This is a test email." '.Display .Send End With End Sub
Dmitry Streb.. 8
你不能将多个名字传递给Recipients.Add - 你得到的名字一个收件人"name@domain.com; name2@domain.com; name3@domain.com".为每个收件人调用Recipients.Add 3次或设置To属性 - 它将解析多个名称.