'———————— REFERENCES ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
' Microsoft Outlook XX.0 Object Library
Sub EnvoyerEmail()
'par Excel-Malin.com ( https://excel-malin.com )
On Error GoTo EnvoyerEmail_Erreur
Dim oOutlook As Outlook.Application
Dim WasOutlookOpen As Boolean
Dim oMailItem As Outlook.MailItem
Dim Body As Variant
Dim Filename1 As String
Dim Subject As String
Dim sFolder As String
sFolder = AddBackslash(GetParam("Path.Pdf", DossierSpecial(Bureau)))
If Not (fsoFolderExist(sFolder)) Then
DisplayErr sFolder, FileNoFound
UserForm1.Show
End If
Filename1 = sFolder & _
fsoFileExt(ThisWorkbook.Name, efFile) & " " & Range("Semaine") & ".pdf"
Subject = fsoFileExt(ThisWorkbook.Name, efFile) & " " & Range("Semaine")
' If fsoFileExist(Filename1) Then
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=Filename1, _
Quality:=xlQualityStandard, IncludeDocProperties:=False, _
IgnorePrintAreas:=False, From:=1, To:=1, OpenAfterPublish:=False
' Else
' DisplayErr Filename1, FileNoFound
' GoTo EnvoyerEmail_Exit
' End If
' Body = "<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">" & _
' "<HTML><HEAD>" & _
' "<META http-equiv=Content-Type content=""text/html; charset=iso-8859-1"">" & _
' "<META content=""MSHTML 6.00.2800.1516"" name=GENERATOR></HEAD>" & _
' "<BODY><DIV STYLE=""font-size: 12px; font-face: Book Antiqua;"">" & "Bonjour Corine," & "<br>" & _
' "Ci-joint la feuille de pointage sur Carrefour Claira, pour la semaine " & Range("Semaine") & "<br><br>" & "Cordialement, " & _
' StrConv(Range("Prénom"), vbProperCase) & "</DIV></BODY></HTML>"
Body = "<H3> <B> CENTRE COMMERCIAL CARREFOUR CLAIRA: Remplaçant M. " & _
Range(" Nom") & Range("Prénom") & " </B> </H3>" & _
"Bonjour Corine, <br>" & _
"Ci-joint les documents pour la semaine 25" & "<br> <br>" & _
"Cordialement, " & Range("Prénom")
'Application_ItemSend
'Préparer Outlook
PreparerOutlook oOutlook
Set oMailItem = oOutlook.CreateItem(0)
Dim SendToCopy As String: SendToCopy = "Tata@laposte.net"
Dim SendTo As String: SendTo = "Tutu@free.fr"
Dim SendFrom As String: SendFrom = "Monadresse@monprovider.fr"
Dim SigString As String
Dim Signature As String
' 'Récupération de la signature
' SigString = AddBackslash(Environ("appdata")) & "Microsoft\Signatures\toto.htm"
' // pour la signature ne fonctionne que si un seul compte est paramètré'
' If Dir(SigString) <> "" Then
' Signature = GetBoiler(SigString)
' Else
' Signature = ""
' End If
'Création de l'email
With oMailItem
'.SentOnBehalfOfName = "Travail"
'.Sender = Range("Sender")
'.From = SendFrom
.To = SendTo
If SendToCopy <> "" Then .CC = SendToCopy
.Subject = Subject
'CHOIX DU FORMAT
'----------------------
'email formaté comme texte
' .BodyFormat = olFormatRichText
' .Body = Body
'ou
'email formaté comme HTML
'.BodyFormat = olFormatHTML
.HTMLBody = Body & "<BR> <BR>" & .HTMLBody 'Signature
.Attachments.Add Filename1
' // si on veux l'afficher
'.Display '<- affiche l'email (si vous ne voulez pas l'afficher, mettez cette ligne en commentaire)
'.Save '<- sauvegarde l'email avant l'envoi (pour ne pas le sauvegarder, mettez cette ligne en commentaire)
'.Send '<- envoie l'email (si vous voulez seulement préparer l'email et l'envoyer manuellement, mettez cette ligne en commentaire)
End With
EnvoyerEmail_Exit:
If (Not (oMailItem Is Nothing)) Then Set oMailItem = Nothing
If (Not (oOutlook Is Nothing)) Then Set oOutlook = Nothing
Exit Sub
EnvoyerEmail_Erreur:
MsgBox "Le mail n'a pas pu être envoyé..." & vbNewLine & Err, vbCritical, "Erreur"
Resume EnvoyerEmail_Exit
End Sub
Private Sub PreparerOutlook(ByRef oOutlook As Object)
'par Excel-Malin.com ( https://excel-malin.com )
'Ce code vérifie si Outlook est prêt à envoyer des emails... Et s'il ne l'est pas, il le prépare.
On Error GoTo PreparerOutlookErreur
On Error Resume Next
'vérification si Outlook est ouvert
Set oOutlook = GetObject(, "Outlook.Application")
If (Err.Number <> 0) Then 'si Outlook n'est pas ouvert, une instance est ouverte
Err.Clear
Set oOutlook = CreateObject("Outlook.Application")
Else 'si Outlook est ouvert, l'instance existante est utilisée
Set oOutlook = GetObject("Outlook.Application")
oOutlook.visible = True
End If
Exit Sub
PreparerOutlookErreur:
MsgBox "Oups..." & vbNewLine & "Nous n'avons pas pu charger Outlook !"
End Sub
Function GetBoiler(ByVal sFile As String) As String
'Dick Kusleika
Dim fso As Object
Dim ts As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(sFile).OpenAsTextStream(1, -2)
GetBoiler = ts.ReadAll
ts.Close
End Function