vba - 从 Excel VBA 发送电子邮件 - 名称无法识别

标签 vba excel outlook

我正在使用以下代码使用 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

奇怪的是,如果我将新消息打开两到三分钟,名称会自动解析:

Working 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

最佳答案

您无法将多个名称传递给 Recipients.Add - 您将得到一个名称为“name@domain.com;name2@domain.com;name3@domain.com”的收件人。为每个收件人调用 Recipients.Add 3 次,或者设置 To 属性 - 它将解析多个名称。

关于vba - 从 Excel VBA 发送电子邮件 - 名称无法识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24587322/

相关文章:

jquery - 如何使用 VBA 触发更新网页的事件?

mysql - 组合访问表

vba - 是否可以检查床单的名称是否在附近?

vba - 矩阵的幂

c# - CSV 编码问题 (Microsoft Excel)

excel - 如何通过批处理文件关闭excel宏而不进行任何保存更改?

c# - 由于缺少 `Application` 引用,Outlook 代码编译错误

vba - 如何使用 VBA 在 Outlook 约会中插入 Quick Parts Building Block?

VBA Excel 自动颜色和数值变化

vba - 在 Outlook (VBA) 中打开文件对话框错误 438