2017-06-16 4 views
1

ici j'essaye d'envoyer un courrier à plusieurs destinataires de outlook vba.Problème dans l'envoi du courrier à plusieurs destinataires en même temps à partir de Outlook vba

L'adresse de messagerie du destinataire provient de la colonne A de la feuille Excel. Whne Je cours le code ci-dessous l'erreur "Run Time error 1004; Méthode 'cellules de object'_Global' échoué"

comment envoyer le même courrier à plusieurs destinataires en même temps.

À: [email protected]; [email protected]; [email protected] CC: [email protected]; [email protected] Sujet: Test Mail

Code: 

Sub Sendmail() 
    Dim olItem As Outlook.MailItem 
    Dim xlApp As Excel.Application 
    Dim xlBook As Excel.Workbook 
    Dim xlSht As Excel.Worksheet 
    Dim sPath As String 
    Dim iRow As Long 

     iRow = 1 

    sPath = "XX" 
' // Excel 
    Set xlApp = CreateObject("Excel.Application") 
' // Workbook 
    Set xlBook = xlApp.Workbooks.Open(sPath) 
' // Sheet 
    Set xlSht = xlBook.Sheets("Sheet1") 


Do Until IsEmpty(Cells(iRow, 1)) 

     Recip = Cells(iRow, 1).Value 
    ' subject = Cells(iRow, 2).Value 
    ' Atmt = Cells(iRow, 3).Value ' 

' // Create e-mail Item 
    Set olItem = Application.CreateItem(olMailItem) 

    With olItem 
    Set olRecip = .Recipients.Add(Recip) 




     .CC = xlSht.Range("B1") 

     .subject = "test" 
     .Display 
     .Send 
    End With 


' // Close 
    xlBook.Close SaveChanges:=True 
' // Quit 
    xlApp.Quit 

    '// CleanUp 


     iRow = iRow + 1 

    Loop 
    Set xlApp = Nothing 
    Set xlBook = Nothing 
    Set xlSht = Nothing 
Set olItem = Nothing 



End Sub 
+0

qui offic/fenêtre Courez-vous? – 0m3r

+0

Bureau Windows 8.1 – Sai

Répondre

1

Cela devrait od le travail pour vous.

Faire une liste Sheets (« Sheet1 ») avec:

In column A : Names of the people 
In column B : E-mail addresses 
In column C:Z : Filenames like this C:\Data\Book2.xls (don't have to be Excel files) 

La boucle de volonté macro par chaque ligne « Sheet1 » et s'il y a une adresse e-mail dans la colonne B et le nom du fichier (s) dans la colonne C: Z, il créera un mail avec cette information et l'enverra.

Sub Send_Files() 
'Working in Excel 2000-2016 
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm 
    Dim OutApp As Object 
    Dim OutMail As Object 
    Dim sh As Worksheet 
    Dim cell As Range 
    Dim FileCell As Range 
    Dim rng As Range 

    With Application 
     .EnableEvents = False 
     .ScreenUpdating = False 
    End With 

    Set sh = Sheets("Sheet1") 

    Set OutApp = CreateObject("Outlook.Application") 

    For Each cell In sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants) 

     'Enter the path/file names in the C:Z column in each row 
     Set rng = sh.Cells(cell.Row, 1).Range("C1:Z1") 

     If cell.Value Like "?*@?*.?*" And _ 
      Application.WorksheetFunction.CountA(rng) > 0 Then 
      Set OutMail = OutApp.CreateItem(0) 

      With OutMail 
       .to = cell.Value 
       .Subject = "Testfile" 
       .Body = "Hi " & cell.Offset(0, -1).Value 

       For Each FileCell In rng.SpecialCells(xlCellTypeConstants) 
        If Trim(FileCell) <> "" Then 
         If Dir(FileCell.Value) <> "" Then 
          .Attachments.Add FileCell.Value 
         End If 
        End If 
       Next FileCell 

       .Send 'Or use .Display 
      End With 

      Set OutMail = Nothing 
     End If 
    Next cell 

    Set OutApp = Nothing 
    With Application 
     .EnableEvents = True 
     .ScreenUpdating = True 
    End With 
End Sub 

https://www.rondebruin.nl/win/s1/outlook/amail6.htm