VBA Outlook

Vi har sett VBA i Excel och hur vi automatiserar våra uppgifter i Excel med att skapa makron, i Microsoft Outlook har vi också en referens för VBA och med vilken vi kan styra utsikterna med VBA, detta gör våra upprepade uppgifter i Outlook lättare att automatisera, och liknande excel måste vi göra det möjligt för utvecklarfunktionen att använda VBA i Outlook.

VBA Outlook

Skönheten i VBA är att vi kan referera till andra Microsoft-objekt som PowerPoint, Word och Outlook. Vi kan skapa vackra presentationer, vi kan arbeta med Microsoft Word-dokument och slutligen kan vi också skicka e-postmeddelanden. Ja, du hörde det rätt, vi kan skicka e-post från excel själv. Det låter besvärligt men samtidigt lägger vi också ett leende. I den här artikeln kommer jag att visa dig hur du arbetar med Microsoft Outlook-objekt från Excel med VBA-kodning. Läs vidare…

Hur refererar vi till Outlook från Excel?

Kom ihåg att Outlook är ett objekt och vi måste ställa in referensen till detta i objektreferensbiblioteket. Följ stegen nedan för att ställa in Outlook-objektet så att det refererar.

Steg 1: Gå till Visual Basic Editor.

Steg 2: Gå till Verktyg> Referens.

Steg 3: I nedanstående referensobjekt rullar du nedåt och väljer “MICROSOFT OUTLOOK 14.0 OBJECT LIBRARY”.

Markera rutan för “MICROSOFT OUTLOOK 14.0 OBJECT LIBRARY” för att göra den tillgänglig för Excel VBA.

Nu kan vi komma åt VBA Outlook-objektet från Excel.

Skriv en kod för att skicka e-post från VBA Outlook från Excel

Vi kan skicka e-postmeddelanden från Excel via Outlook-appen. För detta måste vi skriva VBA-koder. Följ stegen nedan för att skicka e-postmeddelanden från Outlook.

Du kan ladda ner denna VBA Outlook till Excel-mall här - VBA Outlook till Excel-mall

Steg 1: Skapa en delprocedur.

Koda:

 Alternativ Explicit Sub Send_Exails () Avsluta Sub 

Steg 2: Definiera variabeln som VBA Outlook.Application .

Koda:

 Alternativ Explicit Sub Send_Exails () Dim OutlookApp som Outlook.Application End Sub 

Steg 3: Ovanstående variabelreferens till VBA Outlook-applikationen. I framtiden måste vi skicka e-post, så definiera en annan variabel som Outlook.MailItem.

Koda:

 Alternativ Explicit Sub Send_Exails () Dim OutlookApp som Outlook.Application Dim OutlookMail som Outlook.MailItem End Sub 

Steg 4: Nu är båda variablerna objektvariabler. Vi måste ställa in dem. Ställ först in variabeln "OutlookApp" som New Outlook.Application .

Koda:

 Sub Send_Exails () Dim OutlookApp som Outlook.Application Dim OutlookMail som Outlook.MailItem Ställ in OutlookApp = New Outlook.Application End Sub 

Steg 5: Ställ nu in den andra variabeln "OutlookMail" enligt nedan.

Ställ in OutlookMail = OutlookApp.CreateItem (olMailItem)

Koda:

 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 

Steg 6: Använd nu Med uttalandeåtkomst VBA Outlook Mail.

Koda:

 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 komma åt alla tillgängliga artiklar med e-postobjekt som “E-postens kropp”, “Till”, “CC”, “BCC”, “Ämne” och många fler saker.

Steg 7: Nu inuti med uttalandet kan vi se IntelliSense-listan genom att sätta en punkt .

Steg 8: Välj först kroppsformatet som olFormatHtml .

Koda:

 Med OutlookMail .BodyFormat = olFormatHTML Slut med 

Steg 9: Nu visa e-postmeddelandet.

Koda:

 Med OutlookMail .BodyFormat = olFormatHTML .Display slut med 

Steg 10: Nu måste vi skriva e-postmeddelandet i e-postmeddelandet. Välj HtmlBody för detta .

Koda:

 Med OutlookMail .BodyFormat = olFormatHTML .Display .HTMLBody = "Skriv din e-post här" Avsluta med 

Nedan följer exemplet på texten till e-postskrivningen.

Steg 11: Efter att ha skrivit e-postmeddelandet måste vi nämna mottagarens e-post-id. För denna åtkomst “ Till ”.

Steg 12: Nästa omnämnande för vem du vill CC e-postmeddelandet.

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