попробуйте, не в последнюю очередь добавьте Библиотека объектов Microsoft Outlook X.0 , Чтобы добавить эту библиотеку, следуйте этой процедуре Инструменты -> Ссылки -> Выберите библиотеку Microsoft Outlook X.0 Библиотека объектов -> Нажмите ОК:
Sub SendMail(ByVal Sujet As String, ByVal Destinataire As String, ByVal ContenuEmail As String, Optional ByVal PieceJointe As String)
' Add Microsoft Outlook X.0 Object Library to make the macro work
On Error GoTo EnvoyerEmailErreur
Dim oOutlook As Outlook.Application
Dim WasOutlookOpen As Boolean
Dim oMailItem As Outlook.MailItem
Dim Body As Variant
Dim Destinataire As String
Dim Sujet As String
Dim Body As String
Destinataire = "Test@test.com"
Sujet = "Choose a subject"
Body = "Your email"
If (Body = False) Then
MsgBox "Mail non envoyé car vide", vbOKOnly, "Message"
Exit Sub
End If
PreparerOutlook oOutlook
Set oMailItem = oOutlook.CreateItem(0)
With oMailItem
.To = Destinataire
.Subject = Sujet
.BodyFormat = olFormatHTML
.HTMLBody = "<html><p>" & Body & "</p></html>"
.Display
'.Save
.Send
End With
If (Not (oMailItem Is Nothing)) Then Set oMailItem = Nothing
If (Not (oOutlook Is Nothing)) Then Set oOutlook = Nothing
Exit Sub
EnvoyerEmailErreur:
If (Not (oMailItem Is Nothing)) Then Set oMailItem = Nothing
If (Not (oOutlook Is Nothing)) Then Set oOutlook = Nothing
MsgBox "Le mail n'a pas pu être envoyé...", vbCritical, "Erreur"
End Sub
Private Sub PreparerOutlook(ByRef oOutlook As Object)
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 "Une erreur est survenue lors de l'exécution de PreparerOutlook()..."
End Sub
С уважением