Envoi de mail avec signature en VBA de Access en utilisant Outlook
Bonjour tout le monde,
Tout est dans le titre, je cherche désespérément à envoyer un mail à partir d'Access 2013 via Outlook 2013 avec à la fin une signature.
J'arrive à envoyer des mails mais je bloque pour l'ajout de la signature.
Voici mon code :
Cet macro permet de connaitre l'ID des comptes mail dans outlook, elle a une utilité pour la fonction d'Envoi mail "SendOLMail2" (voir Accounts.Item)
Code:
1 2 3 4 5 6 7 8 9 10 11 12
|
Sub Which_Account_Number()
'Don't forget to set a reference to Outlook in the VBA editor
Dim OutApp As Outlook.Application
Dim i As Long
Set OutApp = CreateObject("Outlook.Application")
For i = 1 To OutApp.Session.Accounts.Count
MsgBox OutApp.Session.Accounts.Item(i) & " : This is account number " & i
Next i
End Sub |
Fonction Envoi mail :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
Public Function SendOLMail2( _
ByVal strEmail As String, _
ByVal strObj As String, _
ByVal strMsg As String, _
ByVal blnEdit As Boolean, _
Optional ByVal avarFichiers As Variant)
Dim ol As Outlook.Application
Dim mi As Outlook.mailItem
Dim varPJ As Variant
On Error GoTo OLMailErr
Set ol = New Outlook.Application
Set mi = ol.CreateItem(olMailItem)
With mi
.To = strEmail
.Subject = strObj
.HTMLBody = strMsg & "<br>" & "<div>" & "<b>" & "<p>" & "<FONT COLOR=#3333FF>" & "<i>" & .HTMLBody & signature
.SendUsingAccount = ol.Session.Accounts.Item(1) 'La fonction Which_Account_Number (plus haut), une fois executer à déclarer que le comtpe "toto@mondomaine.fr" avait l'ID = 1
'Ajout des pièces jointes
For Each varPJ In avarFichiers
.Attachments.Add (varPJ)
Next
.Display 'Afficher le message (indispensable si l'on a rajouter la signature .GetInspector)
.BodyFormat = 2
'.GetInspector.CommandBars.Item("Insert").Controls("Signature").Controls("maSignature").Execute
'T0 = Now + TimeValue("0:00:10")
'Do Until Now > T0
'Loop
'Envoi du message
If Not blnEdit Then
.Send
MsgBox "Message Envoyé !", vbInformation, "Information" ' Un petit message de confirmation
End If
End With
Set mi = Nothing
Set ol = Nothing
Exit Function
OLMailErr:
MsgBox "Erreur : " & Err.Number & vbCrLf & Err.Description
Exit Function
End Function |
J'ai essayé avec ".GetInspector.CommandBars.Item("Insert").Controls("Signature").Controls("maSignature").Execute" mais cela ne semble pas fonctionné j'obtiens l'erreur "Erreur 5 Argument ou appel de procédure incorrect"
Appel de la fonction via un bouton "envoi EMAIL" dans un formulaire
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
Private Sub Commande53_Click()
Dim strSQL As String
Dim strMessageType As String
Dim strTitre As String
Dim strMsg As String
Dim astrFichiers(1 To 1) As String
' Titre du message
strTitre = "[FACTURE]"
' Message type à expédier
strMessageType = "<FONT COLOR=#3333FF>Bonjour </b>," _
& "<p><br /></p> " _
astrFichiers(1) = "D:\maPieceJointe.txt"
' Construire un message personnalisé
strMsg = "coucou"
astrFichiers(1) = "D:\maPieceJointe.txt"
' Expédier le mail
SendOLMail2 "monadresseDeTest@gmail.com", strTitre, strMsg, True, astrFichiers
astrFichiers(1) = "D:\maPieceJointe.txt"
End Sub |
Merci pour votre aide.