⟳ Automation

Mail merge in Word and Writer: letters and labels in bulk

A practical guide to mail merge in Word and LibreOffice Writer: preparing the list in Excel, inserting fields, using rules and avoiding mistakes.

Before writing a script, it is worth asking whether the tool you need isn't already inside the program you use every day. For personalised letters, envelopes and labels the answer is yes: mail merge has existed in Word for decades and is also available in LibreOffice Writer. It combines a main document with a list of recipients, producing one document for each, without a single line of code.

The ingredients

Mail merge has two components:

  • the main document, that is, the letter, envelope or label sheet, with the fixed text and fields in place of the variable data;
  • the data source, usually an Excel or Calc sheet in which each row is a recipient and each column a field.

The quality of the result depends almost entirely on the data source. A messy list produces messy letters.

Preparing the list properly

A few rules that prevent most problems:

  1. A single header row in the first row of the sheet, with short names and no spaces: FirstName, LastName, Address, PostCode, City.
  2. No blank or title rows above the header and no merged cells.
  3. One piece of data per column: keeping first and last names separate lets you write both "Dear Mario" and "Mr Rossi".
  4. Postcodes as text: if the column is numeric, a code such as 00184 becomes 184. Format the column as text before entering the data.
  5. Amounts and dates ready to use: mail merge does not always keep cell formatting. An extra column with a formula such as =TEXT(C2,"#,##0.00") guarantees the amount appears exactly as intended.

The procedure in Word

The commands are on the Mailings tab.

  1. Start Mail Merge and choose the document type: letters, email messages, envelopes, labels.
  2. Select Recipients > Use an Existing List and point to the Excel file and the correct sheet.
  3. Edit Recipient List lets you filter or exclude rows, for example only the clients in one area.
  4. Place the cursor in the text and use Insert Merge Field for each variable item. Fields appear between angle quotes, like «FirstName».
  5. Preview Results shows the letter with real data; the arrows move through the recipients.
  6. Finish & Merge offers three routes: edit individual documents (a single file with all the letters, to check before printing), print directly or send as email.

I always recommend the first option: having the complete document lets you leaf through it and correct any anomalies before they turn into printed paper.

Conditional rules

The Rules button adds logic without programming. The most useful is If... Then... Else: if the Title field is "Ms", write "Dear Madam", otherwise "Dear Sir". Another practical rule is Skip Record If, which skips recipients that don't meet a condition, for instance those without an address.

Rules are convenient but become hard to read as they multiply. When conditions go beyond two or three, it is better to compute the text directly in the sheet with a formula, in a Greeting column, and insert that as a plain field.

Labels and envelopes

For labels you choose the format of the adhesive sheet under Labels > Options, giving the manufacturer and the code printed on the box. Word creates a table with one cell per label; you lay out the first and use Update Labels to copy the layout to all the others. Before printing a pack of adhesive sheets, a test on plain paper held against the label sheet up to the light avoids waste.

The same thing in LibreOffice Writer

In Writer you can use the mail merge wizard, available from the Tools menu, which guides you step by step through choosing the document type, the data source and the fields. The list can be a Calc or Excel sheet; Writer registers it as a data source, and fields can also be inserted by dragging them from the data sources pane, which opens with Ctrl+Shift+F4. The final result can be saved as a single document or as separate files, PDF included.

Need Word Writer
Insert fields Insert Merge Field Data sources pane or wizard
Conditions Rules Conditional fields
Labels Mailings > Labels New > Labels
Single output Edit Individual Documents Save Merged Document

When to switch to a script

Mail merge covers cases where the document has a fixed structure very well. It shows its limits when each recipient has a variable number of rows, such as a statement listing open invoices, or when you need hundreds of separate files with meaningful names. In my work, in those cases, I move to a template generated with Python. For everything else, mail merge is quicker to prepare and easier for colleagues to use.

In short

  • Mail merge combines a main document with a list of recipients, with no code.
  • Look after the data source: one header, one item per column, postcodes as text.
  • Prepare amounts and dates in the sheet so the formatting isn't lost.
  • Use Edit Individual Documents to check everything before printing.
  • For variable structures or many separate files, consider a script.
MM
Marco Marzaduri

Marco Marzaduri works in cybersecurity, digital forensics and web development. A computer science graduate, he combines rigorous technical analysis with attention to the people who use technology.

Read also