Ads 468x60px

Saturday, July 14, 2012

Send mails using Excel VBA and Outlook

Send mails using Excel VBA and Outlook:
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.

Scenario

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.

Features

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.

=DATE(YEAR(B2),MONTH(B2),DAY(B2))

The time has been calculated by converting the actual time of the call into the relevant 30 minute interval.

=INT((TIME(HOUR(B2),MINUTE(B2),SECOND(B2)))/(1/48))*(1/48)

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
Application.ActiveWorkbook.Close

Else

newWB.SaveAs Filename:=xPath, FileFormat:=xlOpenXMLWorkbook, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _

, CreateBackup:=False

Application.ActiveWorkbook.Close

End If

Else

xNewFolder = xDir & xMonth

MkDir xNewFolder

newWB.SaveAs Filename:=xPath, FileFormat:=xlOpenXMLWorkbook, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _

, CreateBackup:=False

Application.ActiveWorkbook.Close

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



currentWB.Activate

Sheets("Email").Visible = True

Sheets("Email").Select

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

Loop

xStp = xStp + 1

Loop



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")

olNs.Logon

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

olMail.Display



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.

0 comments:

Post a Comment