从 Excel VBA 发送电子邮件 - 名称无法识别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24587322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending Emails from Excel VBA - Names Not Recognized
提问by CaptainABC
I am using the below code to send an email from excel using outlook:
我正在使用以下代码使用 Outlook 从 excel 发送电子邮件:
Private Sub SendEmail()
Set OutlookApp = CreateObject("Outlook.Application")
Set OlObjects = OutlookApp.GetNamespace("MAPI")
Set newmsg = OutlookApp.CreateItem(olMailItem)
newmsg.Recipients.Add ("[email protected]; [email protected]; [email protected]")
newmsg.Subject = "Test Mail"
newmsg.Body = "This is a test email."
'newmsg.Display
newmsg.Send
End Sub
The code works just fine, however I get the below error from Outlook when trying to send the email:
该代码工作正常,但是我在尝试发送电子邮件时从 Outlook 收到以下错误:
ErrorScreen http://im58.gulfup.com/GRENlB.png
错误屏幕 http://im58.gulfup.com/GRENlB.png
The strange thing is that if I leave the new message open for two or three minutes the names automatically get resolved:
奇怪的是,如果我将新消息打开两三分钟,名称会自动得到解析:
Working http://im74.gulfup.com/qmOYGQ.png
工作 http://im74.gulfup.com/qmOYGQ.png
However this doesn't suit me as I don't want the message to be displayed before it's sent. I am looking to have it send as soon as I run the code.
但是,这不适合我,因为我不希望消息在发送之前显示。我希望在运行代码后立即发送。
Any suggestions or workarounds will be appreciated.
任何建议或解决方法将不胜感激。
As a side note: I have tried enabling the "Allow commas as email separators" option in outlook, and then using the commas instead of the semicolons, but I am still facing the same problem.
附带说明:我尝试在 Outlook 中启用“允许逗号作为电子邮件分隔符”选项,然后使用逗号而不是分号,但我仍然面临同样的问题。
UPDATE:
更新:
Below is the working code, as per Dmitry Streblechenko'sanswer:
以下是工作代码,根据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 = ("[email protected]; [email protected]; [email protected]")
.Subject = "Test Mail"
.Body = "This is a test email."
'.Display
.Send
End With
End Sub
回答by Dmitry Streblechenko
You cannot pass multiple names to Recipients.Add - you get a single recipient with the name of "[email protected]; [email protected]; [email protected]". Either call Recipients.Add 3 times once for each recipient or set the To property - it will parse multiple names.
您不能将多个名称传递给 Recipients.Add - 您会得到一个名称为“[email protected];[email protected];[email protected]”的收件人。为每个收件人调用 Recipients.Add 3 次或设置 To 属性 - 它将解析多个名称。
回答by Axel Kemper
You should add a call to ResolveAll
to explicitely resolve all recipients.
Otherwise, resolution is done automatically after a short waiting period.
您应该添加一个调用ResolveAll
以明确解析所有收件人。否则,会在短暂的等待期后自动完成解决。
Example:
例子:
Sub CheckRecipients()
Dim MyItem As Outlook.MailItem
Dim myRecipients As Outlook.Recipients
Dim myRecipient As Outlook.Recipient
Set myItem = Application.CreateItem(olMailItem)
Set myRecipients = myItem.Recipients
myRecipients.Add("Aaron Con")
myRecipients.Add("Nate Sun")
myRecipients.Add("Dan Wilson")
If Not myRecipients.ResolveAll Then
For Each myRecipient In myRecipients
If Not myRecipient.Resolved Then
MsgBox myRecipient.Name
End If
Next
End If
End Sub
Code copied from here.
从这里复制的代码。