9

我正在构建一个 Excel 加载项,它将活动工作簿作为 Outlook 电子邮件模板中的附件发送到特定的联系人组。

我已经得到了前两部分来使用下面的代码,但我不确定如何将该.TO字段设置为联系人组。

Public Sub Mail_Reports()
    Dim rng As Range
    Dim OutApp As Object
    Dim OutMail As Object 

    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With

    On Error Resume Next

    Set OutApp = CreateObject("Outlook.Application")

    'Set this line to the path and file name of your template
    Set OutMail = OutApp.CreateItemFromTemplate("C:\Users\moses\AppData\Roaming\Microsoft\Templates\test.oft")
    On Error Resume Next

    With OutMail
        '.TO field should be set to the contact group
        .BCC = ""
        .Attachments.Add ActiveWorkbook.FullName
        .HTMLBody = Replace(OutMail.HTMLBody, strOldPeriod, strNewPeriod)
        .Subject = Replace(OutMail.Subject, strOldPeriod, strNewPeriod)
        'To display the email leave as is;  to send the Email, change to .Send
        .Display    'or Send
    End With

    On Error GoTo 0

    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub
4

3 回答 3

4

只需使用联系人组的名称(以前称为“分发列表”)。按照Ron de Bruin网站上的建议,我刚刚尝试过,它确实有效。

于 2012-04-06T22:03:17.497 回答
1

扩展接受的答案以简单地使用名称,确保联系人组名称不模棱两可。

例如,如果我有两个名为“我的列表”和“我的列表 2”的组。当我尝试手动发送电子邮件并仅在“收件人”框中键入“我的列表”时,Outlook 会显示一个弹出窗口,询问要解决哪个列表。这有点像 Excel 中的自动填充建议。相反,如果我键入“我的列表 2”,Outlook 将准确地知道我想要哪个列表。

同样,当通过 VBA 尝试相同的事情时,Outlook 会感到困惑,并且错误消息不是很清楚:“Outlook 无法识别一个或多个名称”。

我知道的最简单的解决方法是将“我的列表”的名称更改为“我的列表 1”或其他任何完全唯一的名称,其中没有其他列表共享该确切的基本名称。

于 2021-08-17T14:39:04.917 回答
0

为了解析收件人的电子邮件地址或姓名(因此它们不只显示纯文本),您可以执行以下操作。

With OutMail
    '.TO field should be set to the contact group
    .BCC = ""
    .Attachments.Add ActiveWorkbook.FullName
    .HTMLBody = Replace(OutMail.HTMLBody, strOldPeriod, strNewPeriod)
    .Subject = Replace(OutMail.Subject, strOldPeriod, strNewPeriod)
    'To display the email leave as is;  to send the Email, change to .Send
    .Display    'or Send
    If Not .Recipients.ResolveAll Then
        For Each Recipient In .Recipients
            If Not Recipient.Resolved Then
                MsgBox Recipient.Name & " could not be resolved"
            End If
        Next 
    End If
End With
于 2012-10-23T15:55:55.047 回答