2013-02-20 11 views
7

Sto provando a impostare diversi pulsanti su un modulo Excel per inviare email a diversi gruppi di persone. Ho creato diversi intervalli di celle su un foglio di lavoro separato per elencare gli indirizzi email separati. Ad esempio, desidero "Pulsante A" per aprire Outlook e inserire l'elenco di indirizzi e-mail da "Foglio di lavoro: celle D3-D6". Quindi tutto ciò che si deve fare è premere "Invia" in Outlook.Come posso utilizzare Outlook per inviare e-mail a più destinatari in Excel VBA

Ecco il mio codice VBA finora, ma non riesco a farlo funzionare. Qualcuno può dirmi cosa mi manca o cosa non funziona, per favore?

VB:

Sub Mail_workbook_Outlook_1() 
    'Working in 2000-2010 
    'This example send the last saved version of the Activeworkbook 
    Dim OutApp As Object 
    Dim OutMail As Object 

    EmailTo = Worksheets("Selections").Range("D3:D6") 

    Set OutApp = CreateObject("Outlook.Application") 
    Set OutMail = OutApp.CreateItem(0) 

    On Error Resume Next 
    With OutMail 
     .To = EmailTo 
     .CC = "[email protected];[email protected]" 
     .BCC = "" 
     .Subject = "RMA #" & Worksheets("RMA").Range("E1") 
     .Body = "Attached to this email is RMA #" & Worksheets("RMA").Range("E1") & ". Please follow the instructions for your department included in this form." 
     .Attachments.Add ActiveWorkbook.FullName 
     'You can add other files also like this 
     '.Attachments.Add ("C:\test.txt") 

     .Display 
    End With 
    On Error Goto 0 

    Set OutMail = Nothing 
    Set OutApp = Nothing 
End Sub 
+0

è anche possibile utilizzare [Recipient.Add] (http://stackoverflow.com/questions/13019651/automated-email-generation-not-resolving-multiple- destinatari) – SeanC

risposta

13

Devi ciclo attraverso ogni cella dell'intervallo "D3:D6" e costruire la stringa To. Assegnarlo semplicemente a una variante non risolverà lo scopo. EmailTo diventa un array se si assegna l'intervallo direttamente ad esso. Puoi farlo anche tu, ma poi dovrai scorrere l'array per creare la stringa To

È questo che stai provando? (collaudato)

Option Explicit 

Sub Mail_workbook_Outlook_1() 
    'Working in 2000-2010 
    'This example send the last saved version of the Activeworkbook 
    Dim OutApp As Object 
    Dim OutMail As Object 
    Dim emailRng As Range, cl As Range 
    Dim sTo As String 

    Set emailRng = Worksheets("Selections").Range("D3:D6") 

    For Each cl In emailRng 
     sTo = sTo & ";" & cl.Value 
    Next 

    sTo = Mid(sTo, 2) 

    Set OutApp = CreateObject("Outlook.Application") 
    Set OutMail = OutApp.CreateItem(0) 

    On Error Resume Next 
    With OutMail 
     .To = sTo 
     .CC = "[email protected];[email protected]" 
     .BCC = "" 
     .Subject = "RMA #" & Worksheets("RMA").Range("E1") 
     .Body = "Attached to this email is RMA #" & _ 
     Worksheets("RMA").Range("E1") & _ 
     ". Please follow the instructions for your department included in this form." 
     .Attachments.Add ActiveWorkbook.FullName 
     'You can add other files also like this 
     '.Attachments.Add ("C:\test.txt") 

     .Display 
    End With 
    On Error GoTo 0 

    Set OutMail = Nothing 
    Set OutApp = Nothing 
End Sub 
+0

Non dimenticare di andare su Strumenti -> Riferimento -> Libreria oggetti di Microsoft Outlook – easycheese

+0

No non è necessario;) Sto usando Late Binding :) –

+0

Non ho idea di cosa si tratta :) Ho appena eseguito in quel problema. – easycheese

1
ToAddress = "[email protected]" 
ToAddress1 = "[email protected]" 
ToAddress2 = "[email protected]" 
MessageSubject = "It works!." 
Set ol = CreateObject("Outlook.Application") 
Set newMail = ol.CreateItem(olMailItem) 
newMail.Subject = MessageSubject 
newMail.RecipIents.Add(ToAddress) 
newMail.RecipIents.Add(ToAddress1) 
newMail.RecipIents.Add(ToAddress2) 
newMail.Send 
Problemi correlati