Gestern bekam ich eine Anfrage, ob man eine E-Mail aus Excel versenden kann in der das Dokument als Anhang eingefügt ist.
Zu dieser Anforderung kann man folgenden Code verwenden:
Sub Mailversand() Dim Nachricht As Object, OutlookApplication As Object Set OutlookApplication = CreateObject("Outlook.Application") Dim Anhang As String Anhang = ThisWorkbook.FullName Set Nachricht = OutlookApplication.CreateItem(0) With Nachricht .To = "mailadresse@domain.tld" .Subject = "Betreff " .attachments.Add Anhang .Body = "Mailtext" & vbCrLf & vbCrLf .Display '.Mail.Send End With Set OutlookApplication = Nothing Set Nachricht = Nothing End Sub
Wenn man das Apostroph vor dem Befehl „.Mail.Send“ entfernt, wird die Mail direkt versandt.