2012-04-06 3 views
6

Sto creando un componente aggiuntivo di Excel che invia la cartella di lavoro attiva come allegato in un modello di posta elettronica di Outlook a un gruppo di contatti specifico.Come accedere ai gruppi di contatti in VBA di Excel?

Ho ottenuto che le prime due parti funzionino con il codice riportato di seguito, ma non sono sicuro di come impostare il campo .TO in un gruppo di contatti.

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 

risposta

3

basta usare il nome del gruppo di contatto (precedentemente chiamato "liste di distribuzione"). L'ho appena provato, come suggerito sul sito Ron de Bruin's, e funziona.

+0

Ignora, funziona. La mia confusione era che, anche se popolava il campo come valore in chiaro, Outlook era abbastanza intelligente da riconoscerlo come un gruppo di contatti. Ho avuto la risposta da sempre, ma non lo sapevo XD. – Moses

+0

Ho anche notato che non sembra risolvere e dovrebbe averlo menzionato nella mia risposta. Se si preme Controlla nome, lo fa e, come abbiamo scoperto, anche se non lo fa, manda bene. –

+1

Ehi, vecchio lo so, ma quel collegamento è rotto - potresti inserire un frammento pertinente della risposta di Ron qui - supponendo che ce l'hai ancora? – Huw

0

Per risolvere il problema degli indirizzi e-mail o dei nomi dei destinatari (in modo che non visualizzino solo testo normale), è possibile effettuare quanto segue.

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