VBA Outlook - Hvordan sendes e-mails fra Outlook ved hjælp af VBA-kode?

Indholdsfortegnelse

Vi har set VBA i excel, og hvordan vi automatiserer vores opgaver i excel med oprettelse af makroer, i Microsoft Outlook har vi også en reference til VBA, og ved hjælp af hvilke vi kan kontrollere outlook ved hjælp af VBA, gør det vores gentagne opgaver i Outlook lettere at automatisere, svarende til excel er vi nødt til at gøre det muligt for udviklerfunktionen at bruge VBA i Outlook.

VBA Outlook

Skønheden ved VBA er, at vi kan henvise til andre Microsoft-objekter som PowerPoint, Word og Outlook. Vi kan skabe smukke præsentationer. Vi kan arbejde med Microsoft Word-dokument, og endelig kan vi også sende e-mails. Ja, du hørte det rigtigt. Vi kan sende e-mails fra excel selv. Dette lyder akavet, men på samme tid sætter vi også et smil på vores ansigt. I denne artikel vil jeg vise dig, hvordan du arbejder med Microsoft Outlook-objekt fra excel ved hjælp af VBA-kodning. Læs videre…

Hvordan refererer vi til Outlook fra Excel?

Husk, Outlook er et objekt, og vi skal sætte henvisningen til dette i objektreferencebiblioteket. Følg nedenstående trin for at indstille Outlook-objektet til reference.

Trin 1: Gå til Visual Basic Editor.

Trin 2: Gå til Værktøjer> Reference.

Trin 3: I nedenstående referencer, objektbibliotek, rul ned og vælg “MICROSOFT OUTLOOK 14.0 OBJECT LIBRARY”.

Marker afkrydsningsfeltet "MICROSOFT OUTLOOK 14.0 OBJECT LIBRARY" for at gøre det tilgængeligt for Excel VBA.

Nu kan vi få adgang til VBA Outlook-objektet fra excel.

Skriv en kode for at sende e-mails fra VBA Outlook fra Excel

Vi kan sende e-mails fra excel via Outlook-appen. Til dette er vi nødt til at skrive VBA-koder. Følg nedenstående trin for at sende e-mails fra Outlook.

Trin 1: Opret en underprocedure.

Kode:

Mulighed Eksplicit Under Send_Exails () Afslut Under

Trin 2: Definer variablen som VBA Outlook.Application .

Kode:

Mulighed Eksplicit Under Send_Exails () Dim OutlookApp Som Outlook.Applikation Slut Sub

Trin 3: Ovenstående variabelreference til VBA Outlook-applikationen. I Outlook er vi nødt til at sende e-mails, så definer en anden variabel som Outlook.MailItem.

Kode:

Mulighed Eksplicit Sub Send_Exails () Dim OutlookApp som Outlook.Application Dim OutlookMail som Outlook.MailItem End Sub

Trin 4: Nu er begge variabler objektvariabler. Vi er nødt til at indstille dem. Indstil først variablen “OutlookApp” som Ny Outlook.Application .

Kode:

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

Trin 5: Indstil nu den anden variabel, "OutlookMail", som nedenfor.

Indstil OutlookMail = OutlookApp.CreateItem (olMailItem)

Kode:

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 () 'Denne kode er tidligt bindende, dvs. i Værktøjer> Reference> Du har markeret "MICROSOFT OUTLOOK 14.0 OBJECT LIBRARY" Dim OutlookApp som Outlook.Application Dim OutlookMail Som Outlook.MailItem Set OutlookApp = New Outlook.Application Set OutlookMail = OutlookApp. CreateItem (olMailItem) With OutlookMail .BodyFormat = olFormatHTML .Display .HTMLBody = "Dear ABC" & "
" & "
" & "Find den vedhæftede fil" & .HTMLBody 'sidste .HTMLBody inkluderer signatur fra outlook. ''
inkluderer linjeskift s / h to linjer. To = "[email protected]" .CC = "[email protected]" .BCC = "[email protected]; [email protected]" .Subject = " Test mail ".Attachments = ThisWorkbook .Send End With End Sub

Interessante artikler...