Many to one mail merge
The mail merge function in Microsoft Word works only with a flat data file as a one to one merge. There have been various approaches proposed to enable the merging of lists associated with each record - for example invoices where multiple items appear associated with each buyer. Microsoft's own suggested approach is quite complicated and recorded at How to use mail merge to create a list sorted by category Fellow Word MVP Paul Edstein has posted a tutorial on this, with working field codes and a sample Excel data source which you can download from this web site but it can be a tad bewildering and even Paul would concede that creating this type of merge can be intimidating for those unskilled in mail merge.
To overcome this difficulty another Antipodean MVP and regular contributor to this site, Doug Robbins produced an add-in to attempt to simplify the process, which I have modified and added some further functionality. This page documents the use of the add-in.
The add-in is supplied in DOTM format for Word 2007/2010 and includes a modification to the Mailings Tab of the ribbon.
The add-in requires that the data source used for the merge to be in Excel format with Outlook to be the default e-mail application for Windows.
If there are likely to be some rows with no data in some of the fields, all of the cells that represent the data source must be formatted as text. Otherwise, errors can occur when the routine encounters a type of data that it is not expecting.
The data must be sorted by the field that is going to be used as the key field. Not sorting it that way will not cause an error, but the desired result will not be achieved.
The appropriate add-in should be should be extracted to the Word start-up folder. By default the start-up folder location is a hidden location, but it can easily be located by typing %appdata%\Microsoft\Word\Startup in the address bar of Windows Explorer or your browser. When Word is run the add-in will load and the calling command will be added to a custom toolbar or the mailings tab of the ribbon according to version.
For the purpose of explanation of the use of the add-in, a ample merge document and an Excel data source are included in the zip file. An extract of the relevant part of the merge document is included below.
The merge document must be set up as a conventional letter merge.
While testing the e-mail option(s) with the sample data source, configure Outlook not to send messages immediately!
The sample messages will then go to the outbox from where they can be deleted.
With the example document, the key field is OWNER and the child fields are PROP_ID and LEGAL_DESC.
The fields are inserted as shown in the upper illustration and the intended result is shown in the lower illustration of the two illustrations that immediately follow. Note that only one set of each of the “repeating” fields (PROP_ID and LEGAL_DESC) is required in each location where that data may be reproduced.
The next step is to run the macro. The macro is designed to work in conjunction with Outlook as the default email application. If the merge is going to be as the body of a mail merge message, and you have Outlook configured to spell check messages before sending, Then you will see the following warning as each message is sent:
With a data file of more than a few records, this will soon become extremely tedious, so before progressing further, the macro checks the registry to establish whether the spell check option is selected. As it is not possible to disable the spell check using the macro, If it is selected you are presented with the following dialog, which provides the option to cancel and change the setting manually, or to continue without the option to merge to the body of an e-mail message.
When merging to the body of an e-mail message, be aware that the html format used bears little relationship to your Word merge document, as the formatting requirements for Word document and html are entirely different. Ensure that the layout of the messages is correct (see the Word web view) before sending.
If the data source is detached or is of the wrong type of file, or if the merge document type is wrong the user will be presented with a warning message that explains the error i.e.
If all is well the macro presents a userform from which the key field and the repeating fields are selected:
Messages merged to e-mail
An extract from one of the completed messages
An accompanying message
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.
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 ## XXXXX XXXXX 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:
You can avoid this happening by downloading the "Express ClickYes" utility that is available as a free download from:
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.
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:
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 following fashion:
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 recipient:
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!)
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: