Mail Merge to E-mail with Attachments

Article contributed by Doug Robbins

This 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.

Preparations

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

In addition, running the macro will cause the following warning message to be displayed by Outlook for each email message that the macro sends:

Warning that other program is attempting to send an email

 

You can avoid this happening by downloading the "Express ClickYes" utility that is available as a free download from:

http://www.contextmagic.com/express-clickyes/

Express 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.

Setup

You will 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.

The data 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.

I cannot 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.

In the 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:

<<EmailAddress>>

<<Attachment1>>

<<Attachment2>>

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:

<<EmailAddress>>

D:\Documents\JulyReport.doc

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 following fashion:

<<EmailAddress>>

D:\Documents\JulyReport.doc

<<Attachment2>>

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:

bill.smith@nowhere.com

D:\mugshots\billsmith.jpg

D:\resumes\billsmith.doc

joe.blow@nowhere.com

D:\mugshots\jowblow.jpg

D:\resumes\joeblow.doc

Or in the case of a common attachment for each recipient:

bill.smith@nowhere.com

D:\Documents\JulyReport.doc

joe.blow@nowhere.com

D:\Documents\JulyReport.doc

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 saved it.

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

Sub emailmergewithattachments()
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
End If
' Open the catalog mailmerge document
With Dialogs(wdDialogFileOpen)
    .Show
End With
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)
    With oItem
        .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
        Next i
        .Send
    End With
    Set oItem = Nothing
Next j
Maillist.Close wdDoNotSaveChanges
'  Close Outlook if it was started by this macro.
If bStarted Then
    oOutlookApp.Quit
End If
MsgBox Source.Sections.Count - 1 & " messages have been sent."
'Clean up
Set oOutlookApp = Nothing
End Sub

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:

https://wordmvp.com/FAQs/MacrosVBA/CreateAMacro.htm


Click to view Terms of Use page

Click to view Disclaimer page