You are on page 1of 4

How To Send Emails From An Excel Spreadsheet Using VBA

Scripts
Written by Ryan Dube
February 8, 2012

(http://www.makeuseof.com/tag/author/ryandube/)

Using emails as part of any program is a nice way to automate important tasks, and it also
significantly improves the value and functionality of any program or script.
In the past, Ive used email a whole lot in my batch jobs and other automated scripts, just like
Ive described in past articles here on using tools like Sendmail
(http://www.makeuseof.com/tag/send-automated-emails-save-time-sendemail-windows-taskscheduler/) or Blat (http://www.makeuseof.com/tag/easily-quickly-send-command-line-emailsblat/) to issue emails straight from the command line, or from within a command line script.
These are great for those times when you have a script thats monitoring the health of a
computer or the status of a specific process, but what if you want to automate sending emails
from within Office products like Word or Excel?
There are a lot of reasons why you might want to do so. Maybe you have staff that update documents or spreadsheets on a weekly basis,
and youd like to receive an email notification of when those updates take place, and even a report of the data from within those sheets.
There are a few techniques you can use to program automated emails from within Excel, but my favorite remains CDO.

Sending Emails From Within Excel


Youre probably thinking that scripting outgoing email into an Excel VBA script is going to be painfully complicated. Well, thats not the case
at all.
CDO is a messaging component used in Windows for a few generations of the OS. It used to be called CDONTS, and then with the advent
of Windows 2000 and XP, it was replaced with CDO for Windows 2000.This component is already included in your VBA installation within
Word or Excel and its ready for use.
Using the component makes sending emails from within Windows products with VBA extremely easy. In this example, Im going to use the
CDO component in Excel to send out an email that will deliver the results from a specific Excel cell.

The first step is to go to the Developer menu tab, click on Insert in the Controls box, and then select a command button. Draw it into the
sheet and then create a new macro for it.

Sign up for more tips and awesome articles

Email

Let's go

When Excel opens up the VBA editor, youre going to need to add the reference to the CDO library. You can access this in the Tools menu,
and then scroll down the list until you find Microsoft CDO for Windows 2000 Library. Select the checkbox and click OK.

Now youre ready to use CDO to issue emails from inside Excel. To do this, you first need ot create the mail objects and set up all of the
fields that will be required for sending the email. Keep in mind that while many of the fields are optional, the From and To fields are required.

DimCDO_MailAsObject
DimCDO_ConfigAsObject
DimSMTP_ConfigAsVariant
DimstrSubjectasString
DimstrFromasString
DimstrToasString
DimstrCcasString
DimstrBccasString
DimstrBodyAsString

strSubject="ResultsfromExcelSpreadsheet"
strFrom="ryxxxxxx@xxxxxcast.net"
strTo="rdxxxxxx@gmail.com"
strCc=""
strBcc=""
strBody="Thetotalresultsforthisquarterare:"&Str(Sheet1.Cells(2,1))

The cool thing about this is that you an build up any string you want to customize a full email message and assign it to the strBody variable.
Piece together components of the message by using the & string to insert data from any of the Excel sheets right into the email message,
just like Ive shown above.
The next section of code is where you will configure CDO to use any external SMTP server that you want to use. In this case I dont need to
use SSL because my SMTP server doesnt require it. CDO is capable of SSL, but thats outside the scope of this article. If you need to use
SSL, I highly recommend Paul Sadowskis awesome writeup (http://www.paulsadowski.com/wsh/cdo.htm) on using CDO.

SetCDO_Mail=CreateObject("CDO.Message")
OnErrorGoToError_Handling

SetCDO_Config=CreateObject("CDO.Configuration")
CDO_Config.Load1

SetSMTP_Config=CDO_Config.Fields

WithSMTP_Config
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing")=2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver")="smtp.metrocast.net"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport")=25
.Update
EndWith

WithCDO_Mail
Set.Configuration=CDO_Config
EndWith

Now that youve configured the connection to the SMTP server for sending the email, all you have to do is fill in the appropriate fields for the
CDO_Mail object, and issue the Send command. This is how you do that.

CDO_Mail.Subject=strSubject
CDO_Mail.From=strFrom
CDO_Mail.To=strTo
CDO_Mail.TextBody=strBody
CDO_Mail.CC=strCc
CDO_Mail.BCC=strBcc
CDO_Mail.Send

Error_Handling:
IfErr.Description<>ThenMsgBoxErr.Description

So there you have it. There wont be any pop-up boxes or security alert messages, which can happen when you resort to using the Outlook
mail object. CDO simply puts together the email and utilizes your SMTP server connection details to fire off the message. Its probably the
easiest way I know to incorporate email into Word or Excel VBA scripts.
Heres what the message looked like that I received in my inbox.

No hassle just the data straight from within the Excel sheet delivered right to my email account. If youre creative with how you put together
the body string variable with all sorts of data from your Excel sheet, you can just imagine the cool automated email reports that you could put
together. And if you dont want to use a command button, just have the script run on the sheet or application close event.
Can you think up any cool uses for CDO in your own Excel, Access, or Word projects? Share your thoughts and ideas in the comments
section below.

Shutterstock (http://image.shutterstock.com/display_pic_with_logo/285868/285868,1280383213,6/stock-photo-laptop-and-mail-done-in-d-isolated-58049572.jpg)

Share

Tweet

Pin

Stumble

Bookmark

Mail (mailto:?body=http://www.makeuseof.com/tag/send-emails-excel-vba/&subject=How To Send Emails From An


Excel Spreadsheet Using VBA Scripts)

You might also like