We hebben VBA in Excel gezien en hoe we onze taken in Excel automatiseren met het maken van macro's, in Microsoft Outlook hebben we ook een referentie voor VBA en waarmee we Outlook kunnen besturen met VBA, dit maakt onze herhaalde taken in Outlook gemakkelijker te automatiseren, en vergelijkbaar met Excel moeten we de ontwikkelaarfunctie inschakelen om VBA in Outlook te gebruiken.
VBA Outlook
Het mooie van VBA is dat we kunnen verwijzen naar andere Microsoft-objecten zoals PowerPoint, Word en Outlook. We kunnen prachtige presentaties maken, we kunnen werken met Microsoft Word-document en tot slot kunnen we ook de e-mails verzenden. Ja, je hebt het goed gehoord, we kunnen e-mails versturen vanuit Excel zelf. Dit klinkt ongemakkelijk, maar tovert tegelijkertijd ook een glimlach op ons gezicht. In dit artikel zal ik u laten zien hoe u vanuit Excel met Microsoft Outlook-object kunt werken met behulp van VBA-codering. Lees verder…
Hoe verwijzen we naar Outlook vanuit Excel?
Onthoud dat Outlook een object is en dat we de verwijzing hiernaar moeten instellen in de objectreferentiebibliotheek. Volg de onderstaande stappen om het Outlook-object als referentie in te stellen.
Stap 1: Ga naar Visual Basic Editor.
Stap 2: Ga naar Tools> Reference.
Stap 3: In de onderstaande objectbibliotheek met referenties scroll naar beneden en selecteer “MICROSOFT OUTLOOK 14.0 OBJECTBIBLIOTHEEK”.
Vink het vakje "MICROSOFT OUTLOOK 14.0 OBJECT LIBRARY" aan om het beschikbaar te maken voor Excel VBA.
Nu hebben we toegang tot het VBA Outlook-object vanuit Excel.
Schrijf een code om e-mails te verzenden vanuit VBA Outlook vanuit Excel
We kunnen de e-mails van Excel verzenden via de Outlook-app. Hiervoor moeten we VBA-codes schrijven. Volg de onderstaande stappen om de e-mails vanuit Outlook te verzenden.
U kunt deze VBA Outlook naar Excel-sjabloon hier downloaden - VBA Outlook naar Excel-sjabloon
Stap 1: Maak een subprocedure.
Code:
Optie Expliciete sub Send_Exails () End Sub
Stap 2: Definieer de variabele als VBA Outlook.Application .
Code:
Optie Expliciete sub Send_Exails () Dim OutlookApp As Outlook.Application End Sub
Stap 3: De bovenstaande variabele verwijzing naar de VBA Outlook-applicatie. In Outlook moeten we e-mails verzenden, dus definieer een andere variabele als Outlook.MailItem.
Code:
Optie Expliciete sub Send_Exails () Dim OutlookApp As Outlook.Application Dim Outlook Mail As Outlook.MailItem End Sub
Stap 4: Nu zijn beide variabelen objectvariabelen. We moeten ze instellen. Stel eerst de variabele “OutlookApp” in als New Outlook.Application .
Code:
Sub Send_Exails () Dim OutlookApp As Outlook.Application Dim OutlookMail As Outlook.MailItem Set OutlookApp = New Outlook.Application End Sub
Stap 5: Stel nu de tweede variabele "OutlookMail" in zoals hieronder.
OutlookMail = OutlookApp.CreateItem (olMailItem) instellen
Code:
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
Stap 6: Nu gebruiken Met toegang tot instructie 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) Met OutlookMail End With End Sub
Nu hebben we toegang tot alle items die beschikbaar zijn met e-mailitems zoals "Hoofdgedeelte van de e-mail", "Aan", "CC", "BCC", "Onderwerp" en nog veel meer dingen.
Stap 7: Nu kunnen we in de met de instructie de IntelliSense-lijst zien door een punt te plaatsen .
Stap 8: Selecteer eerst het body-formaat als olFormatHtml .
Code:
Met OutlookMail .BodyFormat = olFormatHTML Einde met
Stap 9: Nu geven de e-mail.
Code:
Met OutlookMail .BodyFormat = olFormatHTML .Display End With
Stap 10: Nu moeten we de e-mail in de hoofdtekst van de e-mail schrijven. Selecteer hiervoor HtmlBody .
Code:
Met OutlookMail .BodyFormat = olFormatHTML .Display .HTMLBody = "Schrijf hier uw e-mail" Eindig met
Hieronder ziet u het voorbeeld van de hoofdtekst van de e-mail.
Stap 11: Na het schrijven van de e-mail moeten we het e-mailadres van de ontvanger vermelden. Voor deze toegang “ Aan ”.
Stap 12: Geef vervolgens aan voor wie u de e-mail wilt CC .
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