Mail Merge to E-mail with Attachments
Article contributed by
procedure can be used to mail merge to e-mail, including attachments with each
message when Microsoft Office Outlook is installed. It has been used with all
versions of Office from Office 97 up to and including Office 2007.
It is not necessary for Outlook to be
nominated as the default mail program, but it must be installed on the system.
The procedure can handle multiple attachments for each message, individual
attachments for each recipient, common attachments for all recipients, or a
mixture of both.
To run the
macro in this procedure it is necessary to set a reference to the Microsoft
Office Outlook Object Library. You do this from within the Visual Basic Editor,
by selecting References from the Tool menu and then checking the item Microsoft
Office Outlook ##.0 Object Library (where ## is the Outlook version number).
running the macro will cause the following warning message to be displayed by
Outlook for each email message that the macro sends:
You can avoid this happening by downloading the "Express
ClickYes" utility that is available as a free download from:
ClickYes is a tiny
program that sits in the taskbar and clicks the Yes button on behalf of you,
when Outlook's Security Guard opens prompt dialog saying that a program is
trying to send an email with Outlook or access its address book. You can
suspend/resume it by double-clicking its taskbar icon. Developers can automate
its behaviour by sending special messages.
need to create a separate Catalog (or in Word 2002 and later, Directory) type
mail merge main document which creates a word document containing a table in
each row of which would be data from the data source that contains the email
address in the first column and the Drive:\Path\Filename of each attachment in
the second and any subsequent columns, one attachment per cell.
source for this Catalog/Directory type mail merge must be the same data source
as that which you have, or will use for creating the mail merge that you want to
email with attachments.
emphasise too strongly that the path and filenames for the attachments must be
IDENTICAL to the actual path and filenames.
If they are not, the files will not be attached.
A single space in the wrong place is all that it will take for the
procedure to fail.
Catalog/Directory type mail merge main document, insert a one row table into the
cells of which you insert the relevant merge fields from the data source,
similar to the following:
If the same attachment(s) are to be sent to all of
the recipients, and they are not included in the datasource, the
Drive\Path\Filename of the attachment(s) can be typed directly in the cell(s) of
the Catalogue/Directory mail merge main document in place of the <<Attachment>>
fields. In this case, your main document would look like:
You can also have a mixture of a standard attachment
for each recipient and an individual one by setting the main document up in the
After creating this Catalog/Directory mail merge
main document, execute the mail merge to a new document, which if you have set
it up correctly will produce a document containing a table similar to the
following with as many rows as there are records in the data source:
Or in the case of a common attachment for each
Save that file and close it.
When you run the macro that is provided below, you will be asked to open
that document, so remember the name that you have given to it and where you
Then execute to a new document the mail merge that
you want to send out by email with the attachments and with the result of the
execution of that mail merge on the screen, run a macro containing the following
code. (This code is designed to be
pasted into the VBA Editor: it is much easier to read if you do that!)
Dim Source As Document, Maillist As Document, TempDoc As Document
Dim Datarange As Range
Dim i As Long, j As Long
Dim bStarted As Boolean
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem
Dim mysubject As String, message As String, title As String
Set Source = ActiveDocument
' Check if Outlook is running. If it is not, start Outlook
On Error Resume Next
Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
' Open the catalog mailmerge document
Set Maillist = ActiveDocument
' Show an input box asking the user for the subject to be inserted into the email messages
message = "Enter the subject to be used for each email message." ' Set prompt.
title = " Email Subject Input" ' Set title.
' Display message, title
mysubject = InputBox(message, title)
' Iterate through the Sections of the Source document and the rows of the catalog mailmerge document,
' extracting the information to be included in each email.
For j = 1 To Source.Sections.Count - 1
Set oItem = oOutlookApp.CreateItem(olMailItem)
.Subject = mysubject
.Body = Source.Sections(j).Range.Text
Set Datarange = Maillist.Tables(1).Cell(j, 1).Range
Datarange.End = Datarange.End - 1
.To = Datarange
For i = 2 To Maillist.Tables(1).Columns.Count
Set Datarange = Maillist.Tables(1).Cell(j, i).Range
Datarange.End = Datarange.End - 1
.Attachments.Add Trim(Datarange.Text), olByValue, 1
Set oItem = Nothing
' Close Outlook if it was started by this macro.
If bStarted Then
MsgBox Source.Sections.Count - 1 & " messages have been sent."
Set oOutlookApp = Nothing
If you are not sure what to do with the above code,
see the article "What do I do with macros sent to me by other
newsgroup readers to help me out?” at: