Mike-31
Messages postés
18405
Date d'inscription
Statut
Contributeur
Dernière intervention
5 141
Re,
voila le code complet annoté
Option Explicit
Sub Envoi_Mail ()
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim Sourcewb, Destwb As Workbook
Dim TempFilePath, TempFileName As String
Dim Wb, iMsg, iConf As Object
Dim Flds As Variant
'--------------------------- Constante à renseigner
Const Feuille As Variant = "FORMULAIRE" '------------- nom de la feuille à copier
Const Plage As Variant = "A1:H10" '------------------- plage à copier
Const Dest As Variant = "michel.wwwwwwwwww@free.fr" '- adresse mail du destinataire
Const Exped As Variant = "www.xxxxxxx@free.fr" '------ adresse mail de réponse de l'expéditeur
Const C_Ent As Variant = "SMTP.free.fr" '------------- adresse du SMTP"
Const CC As Variant = "" '---------------------------- adresse mail CC
Const BCC As Variant = "" '--------------------------- adresse mail destinataire pour envoi BCC ou BCI ou CCI
Const NumPort As Variant = 25 '----------------------- n° port du serveur sortant
'--------------------------- Si la connexion nécessite une authentification
Const N_Messag As Variant = "Nom utilisateur messagerie"
Const Pass As Variant = "motdepasse"
On Error GoTo errorHandler
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set Sourcewb = ActiveWorkbook
Set Destwb = Workbooks.Add
Sourcewb.Sheets(Feuille).Range(Plage).Copy Destwb.ActiveSheet.Range("A1")
'--------------------------- Déterminer la version d'Excel et d'extension du fichier utilisé
With Destwb
If Val(Application.Version) < 12 Then
'--------------------------- Excel 97-2003
FileExtStr = ".xls": FileFormatNum = -4143
Else
'--------------------------- Excel 2007-2010
If Sourcewb.Name = .Name Then
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
MsgBox "Votre réponse est NON dans la boîte de dialogue de sécurité"
Exit Sub
Else
Select Case Sourcewb.FileFormat
Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
Case 52:
If .HasVBProject Then
FileExtStr = ".xlsm": FileFormatNum = 52
Else
FileExtStr = ".xlsx": FileFormatNum = 51
End If
Case 56: FileExtStr = ".xls": FileFormatNum = 56
Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
End Select
End If
End If
End With
TempFilePath = Environ$("temp") & "\"
'-------------------------- Nom du classeur expédié avec jour et heure d'envoi
TempFileName = Sourcewb.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")
With Destwb
.SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
.Close savechanges:=False
End With
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
iConf.Load -1 '--------- CDO Source Default
Set Flds = iConf.Fields
With Flds
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = C_Ent '"smtp.free.fr" '"Saisir le SMTP du serveur sortant"
.Item("http://schemas.microsoft.com/cdo/configuration/senduserreplyemailaddress") = Exped '"adresse email de réponse expéditeur"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = NumPort '25 'n° port du serveur sortant
'--------------------------- Si la connexion nécessite une authentification libérer les 3 lignes
' .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic
'ou
' .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
' .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = N_Messag 'ou "Nom utilisateur messagerie"
' .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = Pass 'ou "motdepasse"
.Update
End With
With iMsg
Set .Configuration = iConf
.To = Dest
.CC = CC
.BCC = BCC
.From = Exped
.Subject = [C1].Value
.TextBody = "Bonjour" & " " & [C2].Value & "," & vbCrLf & vbCrLf _
& "Veuillez trouvez ci-joint le " & [C3].Value & "." & vbCrLf & vbCrLf _
& [C4].Value & vbCrLf _
& [C5].Value & vbCrLf _
& [C6].Value & vbCrLf _
& [C7].Value & vbCrLf _
& [C8].Value & vbCrLf _
& [C9].Value & vbCrLf _
& [C10].Value & vbCrLf & vbCrLf _
& [C11]
.AddAttachment TempFilePath & TempFileName & FileExtStr
.Send
End With
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
MsgBox "Le mail a été bien envoyé !" '---------- Confirmation de l'envoi
Exit Sub
'--------------------------- Si erreur on sort de la procédure
errorHandler:
'--------------------------- Description de l'erreur survenue
MsgBox Err.Description
'--------------------------- Si erreur ferme la copie temporaire
For Each Wb In Workbooks
If Left(Wb.Name, 1) <> "Claseur" And Wb.Name <> ThisWorkbook.Name Then
Wb.Close
End If
Next Wb
End Sub
A+
Mike-31
Une période d'échec est un moment rêvé pour semer les graines du savoir.