VBA Send Email From Excel - WallStreetMojo

MenuWallstreetMojoAll Courses search🛒Login

VBA Resources

  • E-Books
  • Career Guides
  • Interview Prep Guides
  • Free Practice Tests
  • Excel Cheatsheets
VBA ResourcesVBA Send Email from ExcelBook IconVBA Basics
  • Toggle IconIntroduction to VBA
    • Right arrowVBA Tutorial
    • Right arrowVBA Examples
    • Right arrowVBA Macros
    • Right arrowVBA Code
    • Right arrowVBA Editor
    • Right arrowVBA Comment Block
  • Toggle IconVBA Syntax and Functions
    • Right arrowVBA Operators
    • Right arrowVBA Option Explicit
    • Right arrowVBA Sub
    • Right arrowVBA ME
    • Right arrowVBA Set Statement
    • Right arrowVBA ByRef
    • Right arrowVBA ByVal
  • Toggle IconVBA Workbook and Projects
    • Right arrowVBA ThisWorkbook
    • Right arrowVBA Workbook
    • Right arrowVBA Workbook Open
    • Right arrowVBA ENVIRON
    • Right arrowVBA Project Password
  • Toggle IconVBA Basic Applications
    • Right arrowVBA Send Email from Excel
    • Right arrowVBA SendKeys
    • Right arrowVBA Outlook
    • Right arrowVBA PowerPoint
    • Right arrowVBA Web Scraping
  • Toggle IconVBA Resources
    • Right arrowExcel VBA Macros Books
VBA BasicsArrow down filled🏅 WSM MEMBERSHIPLogo

ALL COURSES@ADDITIONAL50% OFF

UNLOCK DEAL B_VectorGirl_ImageVBA Send Email from Excel

Publication Date :

21 Jul, 2019

Blog Author :

Jeevan A Y

Edited by :

Ashish Kumar Srivastav

Reviewed by :

Dheeraj Vaidya, CFA, FRM

Share icon

Share

Download template imageDownload FREE VBA Send Email from Excel Template and Follow Along!VBA Send Email Excel Template.xlsm

Table of Contents

arrow

VBA Code to Send Emails From Excel

In VBA, to send emails from Excel, we can automatically automate our mailing feature to send emails to multiple users at a time. However, to do so, we need to remember that we may do it by outlook, another product of outlook, so we need to enable outlook scripting in VBA. Once done, we use the .Application method to use outlook features.

VBA's versatility is just amazing. VBA coders love Excel because by using VBA, we not only can work within Excel. Rather, we can also access other Microsoft tools. For example, we can access PowerPoint, Word, and Outlook by using VBA. So I was impressed when I heard of sending emails from Excel. Yes, it is true. We can send emails from excel. This article will show you how to send emails from Excel with attachments using VBA Coding.

VBA-Send-Email-from-Excel
You are free to use this image on your website, templates, etc.. Please provide us with an attribution link.

Set Reference to Microsoft Office Library

We need to send emails from Outlook. Since Outlook is an external objec, we first need object reference to “Microsoft Outlook 16.0 Object Library.”

1. In VBA, Go to Tools > References.

VBA, Go to Tools - References

2. Now, we will see the object reference library. In this window, we need to set the reference to “Microsoft Outlook 16.0 Object Library.”

VBA, Go to Tools - Microsoft Outlook 16.0 Object Library

3. After setting the object reference, click on “OK.”

Now, we can access Outlook objects in VBA coding.

While VBA can help send emails from Excel, there are multiple other functions that you can perform using Excel VBA. To get an idea of how certain events get automated and triggered using the tools, you can have a look at this Excel Events Automation Using Advanced VBA Functions Course.

13 Easy Steps to Send Emails from Excel

Writing the code to send an email with an attachment from Excel is quite complicated but worth spending some time.

Follow the below steps to write your first email excel macro.

Step #1

Start the sub procedure in VBA.

Code:

Sub SendEmail_Example1()

End Sub

VBA Send Email - Step 1

Step #2

Declare the variable Outlook.Application

Code:

Dim EmailApp As Outlook.Application 'To refer to outlook application

VBA Send Email - Step 2

Step #3

The above variable is an object variable. Therefore, we need to create an instance of a new object separately. Below is the code to create a new instance of the external object.

Code:

Set EmailApp = New Outlook.Application 'To launch outlook application

VBA Send Email - Step 3

Step #4

Now, to write the email, we declare one more variable as “Outlook.MailItem”.

Code:

Dim EmailItem As Outlook.MailItem 'To refer new outlook email

VBA Send Email - Step 4

Step #5

To launch a new email, we need to set the reference to our previous variable as “CreateItem.”

Code:

Set EmailItem = EmailApp.CreateItem(olMailItem) 'To launch new outlook email

VBA Send Email - Step 5

Now, the variable “EmailApp” will launch outlook. In the variable “EmailItem,” we can start writing the email.

Step #6

We need to be aware of our items while writing an email. First, we need to decide to whom we are sending the email. So for this, we need to access the “TO” property.

VBA Send Email - Step 6

Step #7

Enter the email ID of the receiver in double quotes.

Code:

EmailItem.To = "Hi@gmail.com"

VBA Send Email - Step 7

Step #8

After addressing the main receiver, if you would like to CC anyone in the email, we can use the “CC” property.

Code:

EmailItem.CC = "hello@gmail.com"

VBA Send Email - Step 8

Step #9

After the CC, we can set the BCC email ID as well.

Code:

EmailItem.BCC = "hhhh@gmail.com"

VBA Send Email - Step 9

Step #10

We need to include the subject of the email we are sending.

Code:

EmailItem.Subject = "Test Email From Excel VBA"

VBA Send Email - Step 10

Step #11

We need to write the email body using HTML body type.

Code:

EmailItem.HTMLBody = "Hi," & vbNewLine & vbNewLine & "This is my first email from Excel" & _ vbNewLine & vbNewLine & _"Regards," & vbNewLine & _"VBA Coder" 'VbNewLine is the VBA Constant to insert a new line

VBA Send Email - Step 11

Step #12

We are working on if we want to add an attachment to the current workbook. Then, we need to use the attachments property. First, declare a variable source as a string.

Code:

Dim Source As String

VBA Send Email - Step 12

Then in this variable, write ThisWorkbook.FullName after Email body.

Code:

Source = ThisWorkbook.FullName

VBA Send Email - Step 12.5

In this VBA Code, ThisWorkbook is used for the current workbook and .FullName is used to get the full name of the worksheet.

Then, write the following code to attach the file.

Code:

EmailItem.Attachments.Add Source

VBA Send Email - Step 12.2

Step #13

Finally, we need to send the email to the mentioned email IDs. We can do this by using the “Send” method.

Code:

EmailItem.Send

VBA Send Email - Step 13

We have completed the coding part.

Code:

Sub SendEmail_Example1() Dim EmailApp As Outlook.Application Dim Source As String Set EmailApp = New Outlook.Application Dim EmailItem As Outlook.MailItem Set EmailItem = EmailApp.CreateItem(olMailItem) EmailItem.To = "Hi@gmail.com" EmailItem.CC = "hello@gmail.com" EmailItem.BCC = "hhhh@gmail.com" EmailItem.Subject = "Test Email From Excel VBA" EmailItem.HTMLBody = "Hi," & vbNewLine & vbNewLine & "This is my first email from Excel" & _ vbNewLine & vbNewLine & _ "Regards," & vbNewLine & _ "VBA Coder" Source = ThisWorkbook.FullName EmailItem.Attachments.Add Source EmailItem.Send End Sub

Run the above code. It will send the email with the mentioned body with the current workbook as the attachment.

INVESTMENT BANKING RESOURCESLearn the foundation of Investment banking, financial modeling, valuations and more.Join Wallstreetmojo YoutubeYoutubeFREE EXCEL COURSESLearn MS Excel right from scratch. Master excel formulas, graphs, shortcuts with 3+hrs of Video.Join Wallstreetmojo InstagramInstagramFREE FINANCE MODELING IN EXCELLearn Financial Modeling in Excel with this Step by Step Guide (Colgate Case Study)Join Wallstreetmojo LinkedinLinkedInINVESTMENT BANKING RESOURCESLearn the foundation of Investment banking, financial modeling, valuations and more.Join Wallstreetmojo YoutubeYoutubeJoin Wallstreetmojo InstagramInstagram

Từ khóa » Visual Basic Send Email