2012-04-06 26 views
6

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

我已经得到了前两个部分,使用下面的代码,但我不知道如何将.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 

回答

3

只需使用联络小组(以前称为“通讯组列表”)的名称。我只是试了一下,就像在Ron de Bruin's网站上建议的那样,它工作正常。

+0

无视它,它确实有效。我的困惑是,尽管它作为一个纯文本值填充该字段,但Outlook足够聪明,可以将其识别为联系人组。我一直都有答案,但是不知道XD。 – Moses 2012-04-06 22:08:31

+0

我也注意到它似乎没有解决,应该在我的答案中提到它。如果您点击“支票姓名”,就会发现,即使您没有发送,也可以发送。 – 2012-04-06 22:18:23

+1

嘿,老,我知道,但是那个链接被打破了 - 你能否把Ron的答案的相关片段放在这里 - 假设你还有它? – Huw 2016-12-06 14:15:57

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