VBA Send Email From Excel - WallStreetMojo

MenuWallstreetMojoAll Courses searchLoginAll BlogsVBA ResourcesVBA Send Email from ExcelVBA 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

GET: WSM ALL COURSES ACCESSDownload 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 = "[email protected]"

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 = "[email protected]"

VBA Send Email - Step 8

Step #9

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

Code:

EmailItem.BCC = "[email protected]"

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 = "[email protected]" EmailItem.CC = "[email protected]" EmailItem.BCC = "[email protected]" 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.

If you're working with a large contact list, personalizing your emails becomes essential. With VBA, you can automate the process of sending individualized emails directly from your Excel workbook. But if you need to personalize emails further using data from a spreadsheet, you may want to explore how to do mail merge in Excel. Mail merge allows you to create customized emails for each recipient, making your communication more personal and effective.

FINANCIAL MODELING 2-DAY BOOTCAMP @70% OFF – YEAR END SALE

Master Financial Modeling in Just 2 Days | Build and Forecast IS, BS & CF from Scratch | Perfect for Graduates and Professionals Looking to Upskill This SALE.

Join WallStreetMojo YouTube

youtubelogo.webp

INVESTMENT BANKING MASTERY PROGRAM @70% OFF – YEAR END SALE

Experience the Exact Training Used by Top Investment Banks | Learn FM, DCF, LBO, M&A, Accounting & More | Unlock Wall Street-Level Skills This SALE.

Join WallStreetMojo Instagram

insta-wallstreetmojo.webp

CHATGPT & AI FOR EXCEL COURSE @70% OFF – YEAR END SALE

Boost Productivity 10X with AI-Powered Excel | Automate Reports, Eliminate Errors & Advance Your Analytics Skills | Transform Your Workflow This SALE.

Join WallStreetMojo LinkedIn

WSM Linkedin(s).png

EXCEL ALL-IN-ONE BUNDLE COURSE @70% OFF – YEAR END SALE

Master Excel, VBA & Power BI Like a Pro | 70+ Hours of Expert-Led Training | Learn Real-World Applications & Earn Your Certification This SALE.

Join WallStreetMojo Facebook

WSM facebook(s).png

Join WallStreetMojo X

WSM twitter(x)(s).png

Từ khóa » Visual Basic Send Email