Add attachment to outlook mail merge
To add an attachment to an Outlook mail merge, you'll need to use a combination of VBA code and the Outlook Object Model. Here's a step-by-step guide to help you achieve this:
Prerequisites:
- You have Outlook 2010 or later version installed.
- You have a mail merge template set up in Microsoft Word.
- You have the Outlook Object Model reference added to your VBA project.
Step 1: Create a new Outlook VBA project
- Open Outlook and press
Alt + F11
to open the Visual Basic Editor. - In the Editor, click
Insert
>Module
to create a new module. - Name the module, for example, "MailMergeAttachments".
Step 2: Set up the mail merge
- In your Word document, go to the
Mailings
tab >Finish & Merge
>Edit Recipient List
. - In the
Edit Recipient List
dialog box, select the recipients you want to send the email to. - Click
OK
to close the dialog box.
Step 3: Add the attachment
-
In the VBA Editor, add the following code to the module you created:
Sub AddAttachmentToMailMerge() Dim olApp As New Outlook.Application Dim olMail As Outlook.MailItem Dim wdDoc As Word.Document Dim wdMailMerge As Word.MailMerge Dim strAttachment As String ' Set the attachment file path and name strAttachment = "C:\Path\To\Your\Attachment.pdf" ' Set the Word document object Set wdDoc = ActiveDocument ' Set the mail merge object Set wdMailMerge = wdDoc.MailMerge ' Create a new Outlook mail item Set olMail = olApp.CreateItem(0) ' Set the mail item's subject and body olMail.Subject = "Test Mail Merge with Attachment" olMail.Body = "This is a test mail merge with an attachment." ' Add the attachment to the mail item olMail.Attachments.Add strAttachment ' Send the mail item olMail.Send ' Clean up Set olMail = Nothing Set wdMailMerge = Nothing Set wdDoc = Nothing Set olApp = Nothing End Sub
Step 4: Run the VBA code
-
In the VBA Editor, click
Run
>Run Sub/User Form
to execute theAddAttachmentToMailMerge
subroutine. -
The code will create a new Outlook mail item, add the attachment, and send the email to the recipients specified in your Word document.
Tips and Variations:
- You can modify the
strAttachment
variable to point to a different file path and name. - You can add multiple attachments by using a loop to iterate through an array of file paths and names.
- You can customize the mail item's subject, body, and other properties as needed.
- You can use this code as a starting point and modify it to fit your specific requirements.