Mail Merge

This guide will show you how to perform a mail merge in a Word document, replacing specific fields with custom data.

Before you begin

You should get your API key from your UniCloud account.

If this is your first time using UniOffice SDK, follow this guide to set up a local development environment.

Clone the project repository

In your terminal, clone the examples repository. It contains the Go code we will be using for this guide.

git clone https://github.com/unidoc/unioffice-examples

To get the example navigate to the path document/mail-merge folder in the unioffice-examples directory.

cd unioffice-examples/document/mail-merge

The repository contains the mm.docx file and the example code main.go, utilized for this example. Below, you’ll find its initial content.

Mail Before being Merged example

How it works

Lines 3-9 import the UniOffice packages and other required dependencies.

The init function in lines 11-18 authenticates your request with your UNIDOC_LICENSE_API_KEY.

In the main function, between lines 20 and 34, the mm.docx file is opened, and the MergeFields() function is utilized to retrieve all the fields found within the document, displaying them in the console as they are encountered. Subsequently, a map is created to store the values intended to replace the original fields, and the MailMerge() function is employed to perform the substitutions, followed by saving the file.

Run the code

Run this command to do the mail merge:

go run main.go

Sample output

Mail Merged example

Got any Questions?

We're here to help you.