Sending Outlook E-mail Messages from Excel. This article features code samples that you can use to perform various e-mail functions from Microsoft Office Excel by using the Microsoft Office Outlook object model. Ron de Bruin, an Excel Most Valuable Professional (MVP) and a frequent contributor to the newsgroups, provides the samples and add-in. Need help installing Office? See all Office options.
This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.
Summary: Microsoft Excel MVP Ron de Bruin provides several samples and a useful add-in that makes it easy for customers to send items from Excel with Outlook. From sending a workbook to sending items to multiple recipients, the code samples and add-in should become a part of your reference library. (8 printed pages)
Applies to: Microsoft Excel 2000 | Microsoft Excel 2002 | Microsoft Excel 2003 | Microsoft Excel 2007 | Microsoft Excel 2010 | Microsoft Outlook 2000 | Microsoft Outlook 2002 | Microsoft Outlook 2003 | Microsoft Outlook 2007 | Microsoft Outlook 2010
Provided by:Ron de Bruin, Microsoft Excel MVP | Frank Rice, Microsoft Corporation | About the Authors
Contents
Sending Outlook E-mail Messages from Excel
Mailing Workbooks as E-mail Attachments
Mailing Single Sheets as Attachments
Conclusion
Read Part Two: OfficeTalk: Using the Excel Object Model to Send Workbooks and Ranges through E-Mail with Outlook (Part 2 of 2)
Sending Outlook E-mail Messages from Excel
This article features code samples that you can use to perform various e-mail functions from Microsoft Office Excel by using the Microsoft Office Outlook object model. Ron de Bruin, an Excel Most Valuable Professional (MVP) and a frequent contributor to the newsgroups, provides the samples and add-in. You can find many more samples and an excellent add-in (RDBMail Add-in) that gives you several e-mail options on the Ribbon user interface (UI), at Ron’s Web site.
Sending workbooks and workbook components from Excel with Outlook is a frequently requested activity. It is relatively easy to do much of this by using commands on the Ribbon UI. However, you may want more control over what you can send. Doing this by using Microsoft Visual Basic for Applications (VBA) in Excel is where developers can make the biggest impact.
One method to use is the SendMail method in the Excel object model. This method works very well but is limited in what it can do. You can find examples using the SendMail method and a link to download a useful add-in created by Ron de Bruin in the article titled Working with Excel Workbooks and Worksheets in E-Mail.
Note
The code samples that are presented in this article will only work with Microsoft Outlook. They will not work with Microsoft Outlook Express or Microsoft Windows Mail.
You need to be aware of two security features for Outlook 2002, Outlook 2003, Outlook 2007, and Outlook 2010. Note that these features are implemented through a security update in Outlook 2000:
Blocking of a customizable list of file attachments considered unsafe because they can be used to propagate viruses.
Pop-up confirmation dialog boxes that occur when a program accesses address-related properties or attempts to send a message.
For more information about Outlook 2007 and Outlook 2010, see the article titled Code Security Changes in Outlook 2007.
In the following sections, I describe some of the VBA code samples created by Ron de Bruin.
Mailing Workbooks as E-mail Attachments
The following subroutine sends the last saved version of the active workbook in an e-mail message. Change the mail address and subject in the macro before you run the procedure.
The following subroutine sends a newly created workbook as a copy of the ActiveWorkbook object. Before sending the workbook, it is saved with a date-time stamp. After the file is sent, the workbook is deleted from the hard disk drive.
Tip
In the previous example, the file wb2 is opened by using the Workbooks.Open method and then mailed, closed, and finally deleted. It is also possible to perform these operations on the file without first opening it. However, by opening the file, you can add code to perform other operations such as the following statement which inserts text and a date into cell A1. You can also perform other operations such as deleting a worksheet or range in the workbook before sending it.
wb2.Worksheets(1).Range('A1').Value = 'Copy created on ' & Format(Date, 'dd-mmm-yyyy')
wb2.Save
Hotmail Inbox
Early Binding
If you want to use IntelliSense help show you the properties and methods of objects as you type them into the VBA Code Editor, you can use early binding. For more information about early binding, see the Word MVP site.
To add early binding, follow these steps:
First, add a reference to the Outlook Object Library. Open the VBA editor by typing Alt +F11.
On the Tools menu, click References.
Select Outlook xx Object Library where xx is the version number such as the Microsoft Outlook 12.0 Object Library.
Next, replace the following three statements in the code:
With these three statements.
Mailing Single Sheets as Attachments
The following subroutine sends a newly created workbook with just a single sheet as the ActiveSheet object. The procedure saves the workbook before mailing with a date-time stamp. After the file is sent, the workbook is deleted from the hard disk. Change the mail address and subject in the macro before you run the procedure.
Note
For additional information about how to work with different versions of Excel, see the section following this code block.
Note
You can also use the following statement if you know the sheet you want to mail. It does not have to be the active sheet. Sheets('Sheet5').Copy
Additional Information
In the macro, you see that if Val(Application.Version) < 12 is true, the next statement is as follows: FileExtStr = '.xls': FileFormatNum = -4143. This value represents the workbook format for Excel versions 97 through 2003.
However, if you run the code in Excel 2007 or Excel 2010, the code will look at the file format of the parent workbook and save the new file in that format.
If the parent workbook is a macro-enabled file (.xlsm) and there is no code in the new workbook, the new file will be saved as a macro-free file (. xlsx). This way, the e-mail recipient can recognize that this is a macro-free file. If the parent workbook is not an .xlsx, .xlsm, or .xls file, the new workbook will be saved as an Excel binary file (.xlsb).
The following are the main formats in Excel 2007 and Excel 2010:
51 = xlOpenXMLWorkbook (macro-free file . xlsx)
52 = xlOpenXMLWorkbookMacroEnabled (macro-enabled file . Torrent bookreader for mac. xlsm)
50 = xlExcel12 (Excel binary workbook with or without macros .xlsb)
56 = xlExcel8 (Excel version 97 through 2003 format file .xls)
If you always want to save in a specific format, you can replace the following statements in the macro.
With one of these statements.
In addition to saving the workbook in an Excel file format, you can also save the single-sheet workbook to one of the following formats:
Excel Hotmail
Comma separated value file (.csv)
Text file (.txt)
Printable file (.prn.)
You should use one of the following statements, respectively.
If you want to use IntelliSense help show you the properties and methods of objects as you type them into the VBA Code Editor, you can use early binding. See the previous section for more information about early binding.
To use early binding in this procedure, replace these three statements in the code.
With these three statements.
Conclusion
In the second part of this article, you will see the procedures for additional ways to send mail from Excel to Outlook. To continue, read OfficeTalk: Using the Excel Object Model to Send Workbooks and Ranges through E-Mail with Outlook (Part 2 of 2)
Outlook Sign Up Free
About the Authors
Hotmail De Excel
Ron de Bruin is an Excel Most Valuable Professional (MVP) and a frequent contributor to the newsgroups. For more information, see Ron's Excel page.
Frank Rice is a senior programming writer and frequent contributor to the Microsoft Office Developer Center.
An unusual one this one but please read on.If I manually copy a range of cells from an Excel worksheet and then paste
into a Draft email in Outlook (My Outlook 2003 uses HTML as draft format with
Word as editor) the resulting pasted cells look fine - colors and formatting
are maintained.
OK now to do it in VBA from Excel.
I copy the range of cells into Clipboard with :-
Range(Cells(aa, 4), Cells(bb, 17)).Select
Selection.Copy
I then obtain the contents of Clipboard and place into a String variable
with :-
Set MyData = New DataObject
MyData.GetFromClipboard
strClip = MyData.GetText
I then create an Outloook object within VBA and build a Draft message using
strClip as part of the Message body.
This all works OK and the Draft message is created but the resulting pasted
range of cells in the Draft message does not look very good, the values are
mis-aligned and wrapped around with any color formatting is lost. It does not
give me the same pretty result as manually cutting and pasting the cells.
Does anyone know how I can preserve the formatting using the VBA method so
that the resulting pasted cells looks as good as the manual method?
Thanks.