Option
Explicit
Public
Sub
createMail()
Dim
appOut
As
Outlook.Application
Dim
outMail
As
Outlook.MailItem
On
Error
Resume
Next
Set
appOut = GetObject(
"Outlook.Application"
)
If
appOut
Is
Nothing
Then
Set
appOut = CreateObject(
"Outlook.Application"
)
End
If
On
Error
GoTo
0
On
Error
GoTo
errorhandler
If
appOut
Is
Nothing
Then
MsgBox
"Kein Outlook installiert?!"
, vbInformation
GoTo
cleanUp
End
If
Set
outMail = appOut.CreateItem(olMailItem)
With
outMail
.Subject =
"Testmail"
.Recipients.Add
"someone@somedomain.com"
.Attachments.Add
"C:\Users\Public\Pictures\Sample Pictures\Desert.jpg"
.HTMLBody =
"<p>Mailbody mit Bild</p>"
& vbCrLf & _
"<img src='cid:Desert.jpg'"
&
"width='500' height='200'>"
.Display
End
With
On
Error
GoTo
0
cleanUp:
If
Not
appOut
Is
Nothing
Then
Set
appOut =
Nothing
If
Not
outMail
Is
Nothing
Then
Set
outMail =
Nothing
Exit
Sub
errorhandler:
MsgBox
"Es ist leider ein Fehler aufgetreten:"
& vbCrLf & _
"Fehlernummer: "
& Err.Number & vbCrLf & _
"Fehlerbeschreibung: "
& Err.Description, vbExclamation
GoTo
cleanUp
End
Sub