Send mails using Excel VBA and Outlook

Posted on April 23rd, 2012 in Automation , Excel Howtos , Learn Excel , VBA Macros - 40 comments

This is a guest post by Vijay, our in-house VBA Expert.

Send mails using Excel VBA and Outlook - how to

In this article we well learn how to use VBA and Microsoft Outlook to send emails with your reports as attachment.


We have an excel based reporting template for the Customer Service Dashboard. We want to update this template using VBA code to create a static version and email it to a list of people. We will define the recipient list in a separate sheet.


1. Code will automatically create necessary folders to save the output file.
2. Email sheet to contain the list of people who are going to receive the report.
3. Sending mail using Microsoft Outlook, primary target is corporate people who are using Outlook as their mail program.

On our VBA project we would need to add references to the below
1. Microsoft Outlook Object Library
2. Microsoft Scripting Runtime Library
Please note the Outlook library will be available depending on the version of Microsoft Outlook installed on your system, in the example workbook the reference is towards version 14 as available with Outlook 2010. If you have a different version of Outlook installed on your system, you need to point to the correct library installed.

We have assumed the data used to create the report is already available in the sheet called “rawData”.
We have then updated the “rawData” sheet with 2 new columns having the Date and Time.
Date has been calculated in the rawData sheets using the Date Function.
The time has been calculated by converting the actual time of the call into the relevant 30 minute interval.
If you need to setup your report into 15 minutes interval then replace 1/48 with 1/96.
We have then used the COUNTIFS and SUMIFS function to create the data view in the Interval Data sheet.


Understanding the VBA code to send mails

I will be discussing only the key elements of the code here.

Sheets(Array("Cover", "Interval Data", "rawData")).Copy

This list will create a new workbook containing the 3 sheets that we have included within the Array() parameter. If your report has more sheets feel free to add them.

Set objfile = New FileSystemObject

If objfile.FolderExists(xDir & xMonth) Then
If objfile.FileExists(xPath) Then
objfile.DeleteFile (xPath)
newWB.SaveAs Filename:=xPath, FileFormat:=xlOpenXMLWorkbook, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
, CreateBackup:=False

newWB.SaveAs Filename:=xPath, FileFormat:=xlOpenXMLWorkbook, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
, CreateBackup:=False
End If
xNewFolder = xDir & xMonth
MkDir xNewFolder
newWB.SaveAs Filename:=xPath, FileFormat:=xlOpenXMLWorkbook, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
, CreateBackup:=False
End If

The above code checks if the correct folder exists for the report to be saved or not and creates one if not existing. This also takes cares of overwriting the existing report in case you need to re-run the report again during the same day.
Creating the List of recipients

Sheets("Email").Visible = True

strEmailTo = ""
strEmailCC = ""
strEmailBCC = ""

xStp = 1

Do Until xStp = 4
Cells(2, xStp).Select
Do Until ActiveCell = ""
strDistroList = ActiveCell.Value
If xStp = 1 Then strEmailTo = strEmailTo & strDistroList & "; "
If xStp = 2 Then strEmailCC = strEmailCC & strDistroList & "; "
If xStp = 3 Then strEmailBCC = strEmailBCC & strDistroList & "; "
ActiveCell.Offset(1, 0).Select
xStp = xStp + 1

The above code will create the list of people for whom the report is intended. We make use of the Do Until Loop here to update the 3 variables to hold the TO, CC and BCC list. The actual email addresses are captured from the Email sheet of the report template.
Please note: there should be no blanks in the list when you are defining the same.

Set olApp = New Outlook.Application
Dim olNs As Outlook.Namespace
Set olNs = olApp.GetNamespace("MAPI")
Set olMail = olApp.CreateItem(olMailItem)
olMail.To = strEmailTo
olMail.CC = strEmailCC
olMail.BCC = strEmailBCC
olMail.Subject = Mid(xFile, 1, Len(xFile) - 4)
olMail.Body = vbCrLf & "Hello Everyone," _
& vbCrLf & vbCrLf & "Please find attached the " & Mid(xFile, 1, Len(xFile) - 4) & "." _
& vbCrLf & vbCrLf & "Regards," _
& vbCrLf & "Chandoo.Org"

The above code creates a new instance of Outlook and then logs in to your default mailbox, using which we will be sending the mail out to the recipients. We also create the body of the mail and specify the To, CC and BCC list.

olMail.Attachments.Add xPath

Finally we add the attachment to the email we have created and then using the Display method bring it on the screen. You may also use the .Send method to send the mail directly.
That is all the code we needed to create a copy of the report with selected few sheets and then send them out using VBA. There are a lot of other methods using which you may be able to send out mails, however this specifically helps out to create report templates to use within your organization and send out mails.
Do you also use VBA and Other methods to send mails, if yes please share the same for the benefit of everyone.

Download Excel File

Click here to download the file & save it on your system and use it to understand this technique.

Do you use Excel to automate emails?

I often use Excel to automatically email reports & messages. This is quite useful when you have to send a snapshot of a report to a large team, but need to customize the email for each recipient.
What about you? Have you used Excel to automate emails? What is your experience like? Do you use VBA or some other technique? Please share using comments.

More on VBA & Macros

If you want to learn more about using VBA to automate reporting & email tasks, read these:

Join our VBA Classes

If you want to learn how to develop applications like these and more, please consider joining our VBA Classes. It is a step-by-step program designed to teach you all concepts of VBA so that you can automate & simplify your work.

Click here to learn more about VBA Classes & join us.

Do you want to be awesome in Excel?

Free Excel tips book - joining bonus - newsletter

Here is a smart way to become awesome in Excel. Just signup for my Excel newsletter. Every week you will receive an Excel tip, tutorial, template or example delivered to your inbox. What more, as a joining bonus, I am giving away a 25 page eBook containing 95 Excel tips & tricks. Please sign-up below:

Your email address is safe with us. Our policies

More awesome tips for you:

Written by Vijay Sharma
Tags: , , , , , , , ,
Home: Main Page
? Doubt: Ask an Excel Question

40 Responses to “Send mails using Excel VBA and Outlook”

  1. Chris Byham says:

    I’ve written code for Excel to email Commission Statements to Sales People each month – so the commission is firstly calculated using a combination of Access (to manipulate Raw Data from a Mainframe System) and Excel (to put this data into a readable format and calculate commission due – each sales person then has his/her own statement). VBA is then used to run through a list of Sales People, attach the relevant spreadsheet file (sometimes zipping 2 or more files for Sales Team Managers etc) and email to the relevant sales person with a bespoke email message.
    This used to be a manual task and take a small team the best part of an afternoon to manual complete.
    VBA is fantastic in these situations.

    • Kelly says:

      THIS IS EXACTLY WHAT I AM TRYING TO DO ! I need help! Did you use Access to run through the list of people and attach the spreadsheet? Or was this all in excel?

  2. Keijo says:

    Hi Chandoo and once again huge thanks for the website. It’s truelly awesome!

    I was wondering what would be your solution for the notification that Outlook brings up when trying to automatically send the email via VBA? At least I get an notice that a program is trying to automatically send a email on my behalf when I use the .Send method.

  3. Joe Carsto says:

    For many year, I have used a combination of Excel, Access and Outlook to mail various reports to the proper team members. I use a combination of my own VBA code and email code from to help automate the process. I use data from the Excel sheets to determine things like: email suject, email priority, the body of the email, etc. I’ve used this method for so long, I don’t know if I could do it manually any longer.

    • Subu says:

      thanks a ton for the link to this website

      that website is awesome !!! such beautiful programs for sending mail with / without attachments


  4. Patrick says:

    Good stuff! For users who might want to also venture into creating things like Meetings and Task Requests as well as emails, or want to use additional properties/methods associated with the MailItem object, you might want to check this out:

  5. Ken Puls says:

    Hi Vijay,

    Nice stuff, and super handy. I’ve been emailing reports from Excel for years, and finally got tired of writing the full blown code each project.

    I created a class module to make this much easier, which may be of some interest to your users. It’s late bound, so no need to set any references, and works with all versions of Outlook. The frame to set up an email becomes very easy as well.

    You can find it here if you’re interested:

  6. Bryan C says:

    I email several reports and dashboards to several people each day. I prefer to simply send “screenshots” (using greenshot or similar) in the body on the email rather than trying to inserct worksheet ranges and what not particulary when sending charts.

    I have been unable to find a good source of vba to automate this procedure. Everyting I can find only captures worksheet ranges instead of screen captures ( just part of the screen, like when you use the crosshairs in greenshot). If anyone has vba that will capute a partial screenshot based on screen coordinates along with the code to paste that screenshot into an outlook body I would greatly indebted.
    Thanks in advance

  7. dan l says:

    I’ve used the outmail thing for mailing gadgets for quite sometime.

  8. René says:

    I’ve been using a combination of Access, Excel and Outlook to send the reports for some years now. As the data and manipulation of data is straightforward, I always do the manipulation in Access. I have to generate my reports each month, and Access gives me the structure to manipulate without copying formulas etc. Once built, it works for all new added data. And while doing, you build a history. That has proven a good thing as well.
    The final report is created in (or exported to) Excel, and send with Outlook. VBA makes it possible to do this without leaving Access.
    @Keijo: I used to run a little program called ClickYes to get rid of the Outlook notifications. You can call this program from VBA as well. That was in Office 2003 days.
    @Joe: There’s a lot of useful VBA examples around, and Ron de Bruin is one of my favorite places to go, as is Chandoo’s since the last year or so.

  9. Bryan C says:

    Hi Rene,
    I agree that Ron DeBruin’s site has the best source of code all in one place for vba to email excel.

    The one thing missing is the ability to email partial screenshots in an outlook body.
    This is really what I need!

  10. Doug Hoover says:

    I use Excel and VBA with Novel GroupWise to email task update notifications from a spreadsheet we use for task tracking (Thanks to Chandoo a few years ago for some of the ideas from a TODO LIST posting). The program manager or supervisor can add a task to the job list (each person has a separate worksheet in the shared workbook) then select the rows with the new additions and run the macro. The “canned” e-mail is created with an additional message from the manager and sent to the individual and supervisor with only the updated tasks numbered and listed.
    I don’t send the entire worksheet only the contents of the rows and a special few of the columns selected. Not perfect in automation but it works very well manually.
    We have just changed to IBM Lotus Notes and I need to change the VBA code next week, so this post is right on time to keep me thinking straight. I have already found the VBA references I need to change to make NOTES work but it is always good to review the basics.
    Thanks again Chandoo and Vijay.

  11. JP says:

    Chandoo, you really need some kind of syntax highlighter.

    Vijay, thanks for posting this code. I’m confused about a couple of things. Your code has Format(xDate, “mm mmmm yy”) in it, I think you meant Format(xDate, “dd mmmm yy”)? Why do you check if a file exists before deleting it? Also, why do you use FSO instead of ‘Dir’ and ‘Kill’ when you already use ‘MkDir’? Thanks for your time.

    • Vijay Sharma says:


      Thanks for pointing out the “dd”, a typo from me on this.

      As you can see the report was desinged at 30 minutes interval and if you needed to run the report multiple times during the day, it would be unnecessary to create multiple copies of the same, hence the delete code.

      FSO is used to bring another object for people to learn, as a lot of time people do not know what are the objects / libraries that exist and how to make use of them.


  12. Kevin F says:

    I’ve been using Excel to send meeting notices to our annual internal technical conference attendees.  I get an extract of the data from our registration system, then sort the data by technical session.  Each session is 1 or 2 hours, and there are 3 days of sessions. 

    I then use VBA to build a meeting notice and add the attendees names to the meeting notice.  The notice shows the data and time of the session, along with the room location.  The nice part about this is that each technical session is then put in everyone’s calendar. 

    Since I am the originator of the meetings, I can easily send updates to the attendees if anything about the session changes (i.e, room location changes).  Attendees can also see their entire schedule in outlook and on their smart phones.  To do this manually would be nearly impossible.  The VBA program sends out thousands of meeting notices in a few minutes through this automated process, and it works flawlessly.

  13. […] enjoy reading articles about interop between Outlook and Excel using VBA code. So I read the recent guest post on Chandoo's site with interest. I downloaded the sample workbook to study the code, and I suggest you do the same. […]

  14. Ben B says:

    I have used the VBA in Excel 2007 to send emails for bulk emails that I need, has significantly increased my productivity.
    The Outlook version I have is 2007.  For every email that the XLS tries to send I have to “Allow/Deny” is there a way to remove this “security” feature to allow the email/s to be directly sent w/o user intervention?

  15. Luis Boy says:

    Hi everbody, I’m looking for a macro that would allow me to email thet active worksheet but that the email address is not hard coded; something that allows me enter the email address before sending it.

    Any help would be very much appreciated

  16. Ram says:

    Hi sir,

    I have tried for send mail using Excel, but When I am trying to add
    5 mailing ids in the “CC” column it’s showing #value error message.
    Please suggest how to send mail more than 4 or 5 people at a time.


  17. medcare mso says:

    I am really amazed. Today I spent a lot of time searching for something interesting on this topic. Finally I found your blog. Thanks for that!
    <a href=””><b>Medical Billing Services</b></a>

  18. John W says:

    I have several basic email messages saved as xxx.msg files from outlook. I use these as templates for emailing some specific items. I’d like to incorporate the use of the the .msg file (basically just the body). I can’t seem to get it to work. Any thoughts?

  19. brendadams says:

    Thanks for providing detailed information about email sending though Excel VB & outlook.

    Open Multiple URL

  20. Ameila says:

    Thanks for the post. I have been using a program that i found at They helped me the first time and then when i has reoccurring reports to be done they helped me set up a system that i could use each time a report was needed.

  21. Rayudu says:

    Hi Chandoo,

    Nice one, if we are using lotus notes than what changes needs to made in vba. Please let me know.


  22. Binaya says:

    I have followed tutorial guidelines and succeed to send email thru Outlook express 2010. But when I used same codes and try to send email on Outlook 2007 it is showing error. Can someone help me how to debug error, I am novice and do not understand much concept. I can forward my workbook but dont know how to attach in this forum :-(

  23. Abby says:

    Hi Vijay,

    Cn you please advice what would be the Vba code for sending email from excel to gmail instead of outlook?

  24. […] Send mails using VBA and Outlook – How to send emails using … – This is a guest post by Vijay, our in-house VBA Expert. In this article we well learn how to use VBA and Microsoft Outlook to send emails with your reports as attachment. […]

  25. George says:

    Hi Vijay,

    I have tried to use your vb code in order to send an email from excell, however get the following “Compile error” when trying to run the code:

    “User defined type not defined” and points to the following section in your code:

    Dim olApp As Outlook.Application

    Please note that i am a novice when it comes to the use of VB code in excel, however am able to follow some of the logic if explained in laymens terms:)

    I would very much appreciate your assistance or some direction to resolve.

    kind regards

  26. […] Send mails using VBA and Outlook – How to send emails using … – Ever wondered how we can use Excel to send emails thru Outlook? We can use Excel VBA Macros to send emails to others thru Outlook. Scenario: We have an excel […]

  27. Jerry says:

    I have a VBA code that helps me send all of my reports via e-mail in Excel. Send single worksheet as an attachment from Excel with VBA code: Excellent VBA Code. Works GREAT !!!

    Instead of entering the To: & CC: e-mails manually, I want to pull the e-mail addresses from a separate worksheet called “E-Mail List”. The (a1) has the “To” e-mail address and (b1) has the “CC” e-mail address. I know this is relatively simple to solve but I cannot figure out what I am doing wrong.

    Thank you everyone in advance.

    Wb2.SaveAs FilePath & FileName & xFile, FileFormat:=xFormat
    With OutlookMail
    .To = “”
    .CC = “”
    .BCC = “”
    .Subject = “kte features”
    .Body = “Please check and read this document.”
    .Attachments.Add Wb2.FullName
    End With
    Kill FilePath & FileName & xFile
    Set OutlookMail = Nothing
    Set OutlookApp = Nothing
    Application.ScreenUpdating = True
    End Sub

  28. Anoop Nair says:

    Hi Team,

    I am into MIS reporting and wanted a help in automating a report.
    i have a report in which 73 report need to be send and for each report i want he excel content to be pasted on my mail body and the excel to be attached. for the same i have written 73 macro codes for each individual report.
    Now its a manual activity to send each report daily so i wanted to know is there any possible way where i can automate to the fullest way so that it makes my job easier.

    Need your help.


  29. merlan says:

    why i cannot run this there is always an error at the side of declaring the variables…

    in outlook.application

  30. Anjan Barua says:


    Is there any possible way to create a hyperlink of inbox mail with excel, so by click to the excel the particular mail can be opend.

  31. Pallab says:

    I am trying to send an html page embedding it in the mail body. I have used the htmlbody property of mailitem. But when i use the .htm file in this property the actual mail body shows the file not found error. Can anyone pleaee help with a piece of vba code to resolve this?

  32. sandip says:

    i need to reply the outook mail. search mail using subject and earliaset mail. please help. need to do this from vba code on excel macro

Leave a Reply