Aspose.Words

Simple Mail Merge Explained

In order to prepare your template to perform a simple mail merge (without regions, similar to the classic mail merge available in Microsoft Word) you should just insert one or more merge fields in the places you want to be populated with data from the data source.

Let us take a look at the Dinner Invitation demo. It creates a letter for a list of clients defined in the database. The template contains a number of merge fields that are populated from two data sources; in other words, two mail merges are performed one after the other. First, data from the first data source is merged into the template. This data source contains only one row because this is information about the inviter, so the whole document content is not repeated and only the appropriate fields are filled with data.

Then the second mail merge operation is executed. The data source it uses contains information about the clients, and consists of multiple rows (the demo selects the top 5 clients living in the USA). Therefore, the whole template is repeated for each data row and every repeated copy is populated with the corresponding client's data.

As a result, we have a document that consists of five filled-in, complete, and personalized invitation letters (a fragment of the very first one is shown below).

As you can see, it is possible, and sometimes useful to perform more than one merge operation with the same template to add data in stages.

You can insert NEXT fields in the Word document to cause the mail merge engine to select the next record from the data source and continue merging. When the engine encounters a NEXT field, it just selects the next record in the data source and continues merging without copying any content. This can be used when creating documents such as mailing labels.