Option Explicit
Sub CommandButton1_Click()
Dim LeMail As Variant
Dim AdresseMail As String
Dim MailItem As Variant, sFichier As String
Dim Inter As Range, LastRow As Long
Feuil1.Activate
Set LeMail = CreateObject("outlook.Application")
AdresseMail = ActiveCell.Offset(0, 10)
LastRow = Feuil1.Range("A" & Rows.Count).End(xlUp).Row
Set Inter = Application.Intersect(Feuil1.Range(ActiveCell.Address), Feuil1.Range("A2:A" & LastRow))
If Inter Is Nothing Then
MsgBox "Sélectionnez une cellule valide dans la 1ere colonne !", vbOKOnly + vbCritical
Set Inter = Nothing
Set LeMail = Nothing
Exit Sub
End If
Set Inter = Nothing
If AdresseMail <> Empty Then
sFichier = ThisWorkbook.Path & "\" & "Test.pdf"
If ExistenceFichier(sFichier) = False Then
MsgBox "Fichier : " & sFichier & vbCrLf & "Introuvable !", vbOKOnly + vbCritical, "Fichier introuvable"
Set LeMail = Nothing
Exit Sub
End If
On Error GoTo Erreurs
With LeMail.CreateItem(MailItem)
.Subject = "Votre bulletin de salaire"
.To = AdresseMail
.HTMLBody = "Bonjour, " & "<br>" & "Ci-joint votre bulletin de salaire"
'.Display
.Attachments.Add (sFichier)
.Send
End With
Else
MsgBox "Ce client ne dispose pas d'une adresse mail", vbOKOnly + vbCritical, "Pas d'adresse Mail"
End If
Exit Sub
Erreurs:
If (Not (LeMail Is Nothing)) Then Set LeMail = Nothing
MsgBox "Le mail n'a pas été envoyé !", vbOKOnly + vbCritical, "Envoi avorté"
End Sub
Private Function ExistenceFichier(sFichier As String) As Boolean
ExistenceFichier = Dir$(sFichier) <> "" And sFichier <> ""
End Function