VBA Outlook - Jak wysyłać e-maile z Outlooka za pomocą kodu VBA?

Spisie treści

Widzieliśmy VBA w programie Excel i jak automatyzujemy nasze zadania w programie Excel dzięki tworzeniu makr, w programie Microsoft Outlook mamy również odniesienie do VBA i za pomocą którego możemy kontrolować perspektywy za pomocą VBA, dzięki czemu nasze powtarzane zadania w programie Outlook są łatwiejsze do zautomatyzowania oraz podobnie jak w programie Excel, musimy włączyć funkcję programisty do korzystania z VBA w programie Outlook.

VBA Outlook

Piękno VBA polega na tym, że możemy odnosić się do innych obiektów Microsoft, takich jak PowerPoint, Word i Outlook. Potrafimy tworzyć piękne prezentacje. Możemy pracować z dokumentem Microsoft Word, a na koniec możemy również wysyłać e-maile. Tak, dobrze słyszałeś. E-maile możemy wysyłać z samego programu Excel. Brzmi to niezręcznie, ale jednocześnie wywołuje uśmiech na naszej twarzy. W tym artykule pokażę, jak pracować z obiektem Microsoft Outlook z Excela przy użyciu kodowania VBA. Czytaj…

Jak odnosimy się do programu Outlook w programie Excel?

Pamiętaj, że Outlook jest obiektem i musimy ustawić odniesienie do tego w bibliotece odniesień do obiektów. Aby ustawić obiekt programu Outlook jako odniesienie, wykonaj poniższe kroki.

Krok 1: Przejdź do Edytora Visual Basic.

Krok 2: Przejdź do Narzędzia> Odniesienie.

Krok 3: W poniższych odnośnikach, biblioteka obiektów, przewiń w dół i wybierz „MICROSOFT OUTLOOK 14.0 OBJECT LIBRARY”.

Zaznacz pole „MICROSOFT OUTLOOK 14.0 OBJECT LIBRARY”, aby udostępnić je dla Excel VBA.

Teraz możemy uzyskać dostęp do obiektu VBA Outlook z programu Excel.

Napisz kod do wysyłania wiadomości e-mail z VBA Outlook z Excela

E-maile z programu Excel możemy wysyłać za pośrednictwem aplikacji Outlook. W tym celu musimy pisać kody VBA. Wykonaj poniższe czynności, aby wysłać e-maile z programu Outlook.

Krok 1: Utwórz procedurę podrzędną.

Kod:

Option Explicit Sub Send_Exails () End Sub

Krok 2: Zdefiniuj zmienną jako VBA Outlook.Application .

Kod:

Option Explicit Sub Send_Exails () Dim OutlookApp As Outlook.Application End Sub

Krok 3: Powyższa zmienna odnosi się do aplikacji VBA Outlook. W Outlooku musimy wysyłać e-maile, więc zdefiniuj inną zmienną jako Outlook.MailItem.

Kod:

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

Krok 4: Teraz obie zmienne są zmiennymi obiektowymi. Musimy je ustawić. Najpierw ustaw zmienną „OutlookApp” na New Outlook.Application .

Kod:

Sub Send_Exails () Dim OutlookApp As Outlook.Application Dim OutlookMail As Outlook.MailItem Ustaw OutlookApp = New Outlook.Application End Sub

Krok 5: Teraz ustaw drugą zmienną, „OutlookMail”, jak poniżej.

Ustaw OutlookMail = OutlookApp.CreateItem (olMailItem)

Kod:

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

Step 6: Now using With statement access VBA Outlook Mail.

Code:

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

Now we can access all the items available with email items like “Body of the email,” “To,” “CC,” “BCC,” “Subject,” and many more things.

Step 7: Now, inside the with the statement, we can see the IntelliSense list by putting a dot.

Step 8: First, select the body format as olFormatHtml.

Code:

With OutlookMail .BodyFormat = olFormatHTML End With

Step 9: Now display the email.

Code:

With OutlookMail .BodyFormat = olFormatHTML .Display End With

Step 10: Now, we need to write the email in the body of the email. For this, select HtmlBody.

Code:

With OutlookMail .BodyFormat = olFormatHTML .Display .HTMLBody = "Write your email here" End With

Below is the example of the body of the email writing.

Step 11: After writing the email, we need to mention the email id of the receiver. For this access, “To.”

Step 12: Next, mention for whom you want to CC the email.

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 () 'Ten kod jest wcześnie wiążący, tj. W menu Narzędzia> Materiały referencyjne> Zaznaczono "MICROSOFT OUTLOOK 14.0 OBJECT LIBRARY" Dim OutlookApp As Outlook.Application Dim OutlookMail As Outlook.MailItem Set OutlookApp = New Outlook.Application Ustaw OutlookMail = OutlookApp. CreateItem (olMailItem) Z OutlookMail .BodyFormat = olFormatHTML .Display .HTMLBody = "Drogi ABC" & "
" & "
" & "Proszę znaleźć załączony plik" & .HTMLBody 'ostatni .HTMLBody zawiera podpis z programu Outlook. ''
zawiera podziały wierszy czarno-białe dwie linie .To = "[email protected]" .CC = "[email protected]" .BCC = "[email protected]; [email protected]" .Subject = " Mail testowy ".Attachments = ThisWorkbook .Send End With End Sub

Interesujące artykuły...