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

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 hvor vi kan styre outlook ved hjælp af VBA, det gør vores gentagne opgaver i Outlook lettere at automatisere, og 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 oprette smukke præsentationer, vi kan arbejde med Microsoft Word-dokument og endelig kan vi også sende e-mails. Ja, du har hørt 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, at Outlook er et objekt, og vi skal sætte referencen 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.

Du kan downloade denne VBA Outlook til Excel-skabelon her - VBA Outlook til Excel-skabelon

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.Application End 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 som Outlook.Application Dim OutlookMail Som Outlook.MailItem Set OutlookApp = New Outlook.Application Set OutlookMail = OutlookApp.CreateItem (olMailItem) End Sub 

Trin 6: Brug nu Med sætningsadgang VBA Outlook Mail.

Kode:

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

Nu kan vi få adgang til alle de tilgængelige poster med e-mail-emner som "E-mailens brødtekst", "Til", "CC", "BCC", "Emne" og mange flere ting.

Trin 7: Nu inde i med udsagnet kan vi se IntelliSense-listen ved at sætte en prik .

Trin 8: Vælg først kropsformatet som olFormatHtml .

Kode:

 Med OutlookMail .BodyFormat = olFormatHTML Afslut med 

Trin 9: Nu vise e-mailen.

Kode:

 Med OutlookMail .BodyFormat = olFormatHTML .Display slut med 

Trin 10: Nu skal vi skrive e-mailen i e-mailens brødtekst. Vælg HtmlBody til dette .

Kode:

 Med OutlookMail .BodyFormat = olFormatHTML .Display .HTMLBody = "Skriv din e-mail her" Afslut med 

Nedenfor er eksemplet på selve e-mail-skrivningen.

Trin 11: Efter at have skrevet e-mailen skal vi nævne modtagerens e-mail-id. For denne adgang “ Til ”.

Trin 12: Næste omtale for hvem du vil CC e-mailen.

Step 13: Now mention the BCC email id’s,

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 object library of Excel VBA

By setting the reference to the object 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() 'This code is early binding i.e in Tools > Reference >You have check "MICROSOFT OUTLOOK 14.0 OBJECT LIBRARY" Dim OutlookApp As Outlook.Application Dim OutlookMail As Outlook.MailItem Set OutlookApp = New Outlook.Application Set OutlookMail = OutlookApp.CreateItem(olMailItem) With OutlookMail .BodyFormat = olFormatHTML .Display .HTMLBody = "Dear ABC" & "

" & "

" & "Please find the attached file" & .HTMLBody 'last .HTMLBody includes signature from the outlook. ''

includes line breaks b/w two lines .To = "[email protected]" .CC = "[email protected]" .BCC = "[email protected];[email protected]" .Subject = "Test mail" .Attachments = ThisWorkbook .Send End With End Sub