2012-04-06 22 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