Perform a watermark mail merge in Microsoft Word on a Mac with a Perl script

Posted by Bracken King on January 4th, 2011

The mail merge feature in Microsoft Word is a great way to generate a series of similar files with dynamic text fields (for example, when you want to generate copies of a file with different names and addresses). Unfortunately, the feature doesn't work with watermarks. I recently ran across this problem when I was trying to put a watermarked copy number onto a series of documents I needed to print out. The graphic below shows the problem I was trying to solve. 

Obviously, doing this once would be easy by just editing the watermark of the template file (shown on the left). The problem was that I needed to generate >50 copies with different numbers (such as 001 on the right), and editing all those files by hand was a little more tedious than I was interested in.

To get around the problem, I wrote a Perl script that extends the mail merge feature to any text in a Word document. The problem is pretty specific, and the current script has a few limitations (detailed below), but if you ever need to generate a merge in Word including the watermark text, hopefully it will come in handy.

Mail merge in Word
If you've never used a mail merge before, the basic principles are pretty straight forward. The goal is to generate multiple variants of a template document that substitutes in some variable text. Traditionally, the variable text would be the name and address of a number of different people (to generate "personalized" letters or emails to a list of people), thus the name "mail merge." The functionality of a mail merge can be much broader than just personalized greetings, however, and it's definitely worth taking a look at the feature if you haven't already (here's a Microsoft tutorial of how set up a mail merge in Word).

In any event, because watermarks are stored as word art, mail merges don't work with them in Word. Fortunately, the new .docx file format for Word documents is relatively accessible for manipulation, enabling one to use a script to replace a pattern in a document with some target text. So that's basically all the script does. You start by creating a word document with some special keyword in it (e.g., XXREPLACEXX), save the document, and then use this script to modify all instances of that keyword with some text of your choosing.

The script
Before going any further, I should mention a few caveats. First off, the script only works on template documents saved in the .docx file format, available in recent versions of Word. Secondly, the script uses a few standard command line tools, so while it should work out of the box on a Mac or Linux system, it won't work in Windows without additional software (such as Cygwin). Finally, the script itself only generates one file at a time, so if you want to perform a series of merges, you'll need to do a little extra work. I'll show an example of that at the end of the post.

To use the script, start by downloading it from here (right click on that link and choose "Save As..." if the direct link is causing trouble) and keep track of where you save it (e.g., on your Desktop). You'll also need a template Word document, either by creating your own, or by downloading and modifying this one. Once you've downloaded the script and created a template, open a terminal window (such as Terminal.app in the Applications folder on a Mac) and change directories to the location of the script. If you saved the script to your Mac Desktop, use the command:

cd ~/Desktop

You then need to make sure the script is executable with the command:

chmod +x ./replaceText_Word.pl

To run the script to generate a new file with the substituted text (e.g., Copy #001), use the command:

./replaceText_Word.pl template.docx out.docx 'Copy #001'

This command will replace all occurrences of 'XXREPLACEXX' in template.docx with the phrase 'Copy #001' (even if it shows up in a watermark), and save the results to out.docx. If you want to use a different replacement pattern (rather than XXREPLACEXX), feel free; just type in the pattern as an additional input to the script (after 'Copy #001').

Wrapping the script
With the above commands, you should be able to manually create a series of documents with modified text. If you want to generate a lot of files, however, you might want to write a little wrapper around the core script. For example, to generate 5 different copies, numbered 001 to 005, you can use a for loop in the terminal:

for i in {1..5}; \
     do ./replaceText_Word.pl template.docx out00$i.docx "Copy #00$i"; \
done;

If you've done a little scripting before, you can easily imagine writing basic wrappers to perform more complicated merges with this (even more advanced than what word supports). If not, the above commands should be enough to get a functional watermark merge. If you run across any problems, feel free to let me know in the comments. Enjoy.

Share this article:

This article was written by...
Bracken King
Co-founder of Less Annoying CRM
We don’t have comments enabled on this article, but I'd be happy to discuss it with you. You can connect with me on: Twitter | Google+

Read more articles by Bracken   Browse all articles on this site
 
The articles on this site are written by members of the Less Annoying CRM team.

Subscribe: RSS | Twitter

Browse all articles

Share this article: