Vi har sett VBA i excel og hvordan vi automatiserer oppgavene våre i excel med å lage makroer, i Microsoft Outlook har vi også en referanse for VBA og bruker som vi kan kontrollere Outlook med VBA, dette gjør våre gjentatte oppgaver i Outlook enklere å automatisere, og ligner på excel, vi trenger for å gjøre det mulig for utviklerfunksjonen å bruke VBA i Outlook.
VBA Outlook
Det fine med VBA er at vi kan referere til andre Microsoft-objekter som PowerPoint, Word og Outlook. Vi kan lage vakre presentasjoner, vi kan jobbe med Microsoft Word-dokument, og til slutt kan vi også sende e-postene. Ja, du hørte det riktig, vi kan sende e-post fra excel selv. Dette høres vanskelig ut, men setter samtidig et smil på ansiktet vårt også. I denne artikkelen vil jeg vise deg hvordan du kan jobbe med Microsoft Outlook-objekt fra å utmerke ved hjelp av VBA-koding. Les videre…
Hvordan refererer vi til Outlook fra Excel?
Husk at Outlook er et objekt, og vi må sette referansen til dette i objektreferansebiblioteket. Følg trinnene nedenfor for å angi at Outlook-objektet skal referere.
Trinn 1: Gå til Visual Basic Editor.
Trinn 2: Gå til Verktøy> Referanse.
Trinn 3: I referanseobjektet nedenfor refererer du til biblioteket og velger “MICROSOFT OUTLOOK 14.0 OBJECT LIBRARY”.
Merk av i “MICROSOFT OUTLOOK 14.0 OBJECT LIBRARY” for å gjøre den tilgjengelig for Excel VBA.
Nå kan vi få tilgang til VBA Outlook-objektet fra excel.
Skriv en kode for å sende e-post fra VBA Outlook fra Excel
Vi kan sende e-post fra Excel gjennom Outlook-appen. For dette må vi skrive VBA-koder. Følg trinnene nedenfor for å sende e-post fra Outlook.
Du kan laste ned denne VBA Outlook til Excel-malen her - VBA Outlook til Excel-mal
Trinn 1: Opprett en underprosedyre.
Kode:
Alternativ Eksplisitt Sub Send_Exails () Slutt Sub
Trinn 2: Definer variabelen som VBA Outlook . Applikasjon .
Kode:
Alternativ Eksplisitt Sub Send_Exails () Dim OutlookApp Som Outlook.Application End Sub
Trinn 3: Ovenstående variabel referanse til VBA Outlook-applikasjonen. I utsiktene må vi sende e-post, så definer en annen variabel som Outlook.MailItem.
Kode:
Alternativ Eksplisitt Sub Send_Exails () Dim OutlookApp som Outlook.Application Dim OutlookMail Som Outlook.MailItem End Sub
Trinn 4: Nå er begge variablene objektvariabler. Vi må sette dem. Sett først variabelen “OutlookApp” som New Outlook.Application .
Kode:
Sub Send_Exails () Dim OutlookApp som Outlook.Application Dim OutlookMail som Outlook.MailItem Sett OutlookApp = New Outlook.Application End Sub
Trinn 5: Sett nå den andre variabelen “OutlookMail” som nedenfor.
Sett 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
Trinn 6: Bruk nå With statement access 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
Nå kan vi få tilgang til alle elementene som er tilgjengelige med e-postelementer som "Body of the email", "To", "CC", "BCC", "Subject" og mange flere ting.
Trinn 7: Nå inne i uttalelsen kan vi se IntelliSense-listen ved å sette en prikk .
Trinn 8: Velg først kroppsformatet som olFormatHtml .
Kode:
Med OutlookMail .BodyFormat = olFormatHTML Slutt med
Trinn 9: Nå viser e-posten.
Kode:
Med OutlookMail .BodyFormat = olFormatHTML .Display Slutt med
Trinn 10: Nå må vi skrive e-posten i e-postteksten. For dette velger du HtmlBody .
Kode:
Med OutlookMail .BodyFormat = olFormatHTML .Display .HTMLBody = "Skriv din e-post her" Avslutt med
Nedenfor er eksemplet på brødteksten til e-postskrivingen.
Trinn 11: Etter å ha skrevet e-posten, må vi nevne e-post-ID-en til mottakeren. For denne tilgangen " Til ".
Trinn 12: Neste omtale for hvem du vil CC e-posten.
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