VBA Outlook - Wie sende ich E-Mails aus Outlook mit VBA-Code?

Inhaltsverzeichnis

Wir haben VBA in Excel gesehen und wie wir unsere Aufgaben in Excel durch das Erstellen von Makros automatisieren. In Microsoft Outlook haben wir auch eine Referenz für VBA, mit der wir Outlook mithilfe von VBA steuern können. Dies erleichtert die Automatisierung unserer wiederholten Aufgaben in Outlook Ähnlich wie bei Excel müssen wir die Entwicklerfunktion aktivieren, um VBA in Outlook verwenden zu können.

VBA-Ausblick

Das Schöne an VBA ist, dass wir auf andere Microsoft-Objekte wie PowerPoint, Word und Outlook verweisen können. Wir können schöne Präsentationen erstellen. Wir können mit Microsoft Word-Dokumenten arbeiten und schließlich auch die E-Mails senden. Ja, du hast es richtig gehört. Wir können E-Mails von Excel selbst senden. Das klingt unangenehm, zaubert aber gleichzeitig ein Lächeln auf unser Gesicht. In diesem Artikel werde ich Ihnen zeigen, wie Sie mit Microsoft Outlook-Objekten aus Excel mithilfe der VBA-Codierung arbeiten. Weiter lesen…

Wie referenzieren wir Outlook aus Excel?

Denken Sie daran, Outlook ist ein Objekt, und wir müssen den Verweis darauf in der Objektreferenzbibliothek festlegen. Führen Sie die folgenden Schritte aus, um das Outlook-Objekt als Referenz festzulegen.

Schritt 1: Wechseln Sie zum Visual Basic-Editor.

Schritt 2: Gehen Sie zu Extras> Referenz.

Schritt 3: Scrollen Sie in den folgenden Referenzen in der Objektbibliothek nach unten und wählen Sie „MICROSOFT OUTLOOK 14.0 OBJECT LIBRARY“.

Aktivieren Sie das Kontrollkästchen "MICROSOFT OUTLOOK 14.0 OBJECT LIBRARY" , um es für Excel VBA verfügbar zu machen.

Jetzt können wir von Excel aus auf das VBA Outlook-Objekt zugreifen.

Schreiben Sie einen Code zum Senden von E-Mails aus VBA Outlook aus Excel

Wir können die E-Mails von Excel über die Outlook-App senden. Dazu müssen wir VBA-Codes schreiben. Führen Sie die folgenden Schritte aus, um die E-Mails aus Outlook zu senden.

Schritt 1: Erstellen Sie eine Unterprozedur.

Code:

Option Explicit Sub Send_Exails () End Sub

Schritt 2: Definieren Sie die Variable als VBA Outlook.Application .

Code:

Option Explicit Sub Send_Exails () OutlookApp als Outlook.Application End Sub dimmen

Schritt 3: Der obige Variablenverweis auf die VBA Outlook-Anwendung. Im Outlook müssen wir E-Mails senden. Definieren Sie daher eine andere Variable als Outlook.MailItem.

Code:

Option Explicit Sub Send_Exails () Dim OutlookApp als Outlook.Application Dim OutlookMail als Outlook.MailItem End Sub

Schritt 4: Nun sind beide Variablen Objektvariablen. Wir müssen sie einstellen. Legen Sie zunächst die Variable "OutlookApp" als " New Outlook.Application" fest .

Code:

Sub Send_Exails () Dim OutlookApp als Outlook.Application Dim OutlookMail als Outlook.MailItem Set OutlookApp = New Outlook.Application End Sub

Schritt 5: Stellen Sie nun die zweite Variable "OutlookMail" wie folgt ein.

Setzen Sie OutlookMail = OutlookApp.CreateItem (olMailItem)

Code:

Sub Send_Exails () Dim OutlookApp als Outlook.Application Dim OutlookMail als Outlook.MailItem Set OutlookApp = New Outlook.Application Set OutlookMail = OutlookApp.CreateItem (olMailItem) End Sub

Schritt 6: Verwenden Sie jetzt With Statement Statement VBA Outlook Mail.

Code:

Sub Send_Exails () Dim OutlookApp als Outlook.Application Dim OutlookMail als Outlook.MailItem Set OutlookApp = New Outlook.Application Set OutlookMail = OutlookApp.CreateItem (olMailItem) Mit OutlookMail End With End Sub

Jetzt können wir auf alle verfügbaren Elemente mit E-Mail-Elementen wie "Text der E-Mail", "An", "CC", "BCC", "Betreff" und vielen weiteren Dingen zugreifen.

Schritt 7: Jetzt können wir innerhalb der mit der Anweisung die IntelliSense-Liste sehen, indem wir einen Punkt setzen .

Schritt 8: Wählen Sie zunächst das Body-Format als olFormatHtml aus .

Code:

Mit OutlookMail .BodyFormat = olFormatHTML End With

Schritt 9: Nun zeigt die E - Mail.

Code:

Mit OutlookMail .BodyFormat = olFormatHTML .Display End With

Schritt 10: Jetzt müssen wir die E-Mail in den Text der E-Mail schreiben. Wählen Sie dazu HtmlBody .

Code:

Mit OutlookMail .BodyFormat = olFormatHTML .Display .HTMLBody = "Schreiben Sie Ihre E-Mail hier" End With

Unten sehen Sie ein Beispiel für den Text des E-Mail-Schreibens.

Schritt 11: Nach dem Schreiben der E-Mail müssen wir die E-Mail-ID des Empfängers angeben. Für diesen Zugriff " An ".

Schritt 12: Als nächstes Erwähnung für wen Sie wollen CC die E - Mail.

Step 13: Now mention the BCC email ids,

Step 14: Next thing is we need to mention the subject for the email we are sending.

Step 15: Now add attachments. If you want to send the current workbook as an attachment, then use the attachment as This workbook.

Step 16: Finally, send the email by using the Send method.

Now, this code will send the email from your VBA outlook mail. Use the below VBA code to send emails from your outlook.

To use the below code, you must set the object reference to “MICROSOFT OUTLOOK 14.0 OBJECT LIBRARY” under the object library of Excel VBA.

By setting the reference to the object, the library is called early binding. The reason why we need to set the reference to object library because without setting the object library as “MICROSOFT OUTLOOK 14.0 OBJECT LIBRARY,” We cannot access the IntelliSense list of VBA properties and methods. This makes the writing of code difficult because you need to be sure of what you are writing in terms of technique and spellings.

Sub Send_Emails () 'Dieser Code ist frühzeitig bindend, dh unter Extras> Referenz> Sie haben das Kontrollkästchen "MICROSOFT OUTLOOK 14.0 OBJECT LIBRARY" aktiviert. OutlookApp als Outlook dimmen. CreateItem (olMailItem) Mit OutlookMail .BodyFormat = olFormatHTML .Display .HTMLBody = "Liebes ABC" & "
" & "
" & "Bitte finden Sie die angehängte Datei" & .HTMLBody 'letzte .HTMLBody enthält Signatur aus dem Outlook. ''
enthält Zeilenumbrüche in zwei Zeilen .To = "[email protected]" .CC = "[email protected]" .BCC = "[email protected]; [email protected]" .Subject = " Testmail ".Attachments = ThisWorkbook .Send End With End Sub

Interessante Beiträge...