【问题标题】:How to access contact groups in Excel VBA?如何访问 Excel VBA 中的联系人组?
【发布时间】:2012-04-20 10:07:25
【问题描述】:

我正在构建一个 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

【问题讨论】:

    标签: vba excel outlook


    【解决方案1】:

    只需使用联系人组的名称(以前称为“分发列表”)。我只是按照Ron de Bruin's 网站上的建议尝试了它,它可以工作。

    【讨论】:

    • 不管它,它确实有效。我的困惑是,即使它将该字段填充为纯文本值,Outlook 也足够聪明,可以将其识别为联系人组。我一直都有答案,但不知道 XD。
    • 我还注意到它似乎没有解决,应该在我的回答中提到它。如果您点击检查名称,它会这样做,正如我们发现的那样,即使您不这样做,它也会发送正常。
    • 嘿,我知道,但是那个链接已经坏了——你能把罗恩的答案的相关 sn-p 放在这里吗——假设你还有它?
    • @Huw,这是一个链接,该链接在页面的中途有一个示例。它看起来确实像输入组名而不是电子邮件地址一样简单。你试过吗?
    • 是的,这很有趣,但对罗恩这个家伙的解决方案感到好奇。总是很高兴有东西可以比较。我最终在 4 月 15 日(在您发布后几天)找到了该网站的备份。不确定是否值得编辑以防万一 - 将来什么时候会丢失?希望对未来的堆栈溢出用户有用! web.archive.org/web/20120415041820/http://www.rondebruin.nl/…
    【解决方案2】:

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

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

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

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

    【讨论】:

      【解决方案3】:

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

      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
      

      【讨论】:

        猜你喜欢
        • 2019-02-22
        • 1970-01-01
        • 2011-05-19
        • 2011-07-11
        • 1970-01-01
        • 2014-06-22
        • 2014-02-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多