Sub Envoi_Mail()
'init
Dim Destin$, Service$, Chemin$, Nom_Fichier$, PathFichier$, NbrDestin%, NbrErreur%
Dim iMsg As Object, iConf As Object
Set iMsg = CreateObject("CDO.Message") '<<< soit ici
Set iConf = CreateObject("CDO.Configuration") '<<< soit ici
NbrDestin = 0: NbrErreur = 0
'boucle envoi
For I = 2 To Sheets(1).Range("d1").End(xlDown).Row
NbrDestin = NbrDestin + 1
Destin = Cells(I, 5)
Service = Cells(I, 2) & "-" & Cells(I, 1)
Chemin = "xxx\aez\"
Nom_Fichier = Cells(I, 4)
PathFichier = Chemin & Nom_Fichier
'test destinataire
If Trim(Destin) = "" Then
MsgBox "Destinataire absent !?", vbExclamation, "envoi mail"
NbrErreur = NbrErreur + 1: GoTo Suivant
End If
'test fichier
If Dir(PathFichier) = "" Then
MsgBox "Fichier non trouvé !" & vbLf & PathFichier & vbLf & Destin & vbLf & Service, vbExclamation, "envoi mail"
NbrErreur = NbrErreur + 1: GoTo Suivant
End If
'envoi
'Set iMsg = CreateObject("CDO.Message") '<<< soit ici
'Set iConf = CreateObject("CDO.Configuration") '<<< soit ici
With iMsg
Set .Configuration = iConf
If .Attachments.Count <> 0 Then .Attachments.Delete (1)
.To = Destin
.CC = ""
.BCC = ""
.From = "zzz@zz.fr"
.Subject = "Comptabilité analytique " & Service
.TextBody = "Bonjour," & Chr(10) & Service
.addattachment PathFichier
.Send
End With
'Set iMsg = Nothing: Set iConf = Nothing 'avec ceci ici
Suivant:
Next
Set iMsg = Nothing: Set iConf = Nothing
MsgBox NbrDestin - NbrErreur & " Mail(s) envoyé(s) !", vbInformation, "envoi mail"
End Sub