Automating the creation of a MS Word document using merge fields

Introduction

The Run Microsoft Word (Merge) is a Format tool used to automate the creation of single or multiple MS Word documents containing merged data.

MS Word documents or templates that contain mail merge fields are imported into the tool. These fields are then read from the document by the tool and presented in a table. Each merge field may then be mapped to by using TaskCentre database columns, variables or formulas which will populate the fields with data.

Alternatively, the fields can be left un-mapped so that the original mail merge data source specified in the document is used instead.

This means that the Run Microsoft Word (Merge) tool can be used to create a document in one of two ways:

  1. Standalone and without the need for a preceding query step, the original mail merge data source is used and one document is generated:7
  2. In-line with a Database Query step where data from the query is mapped to the mail merge fields. This allows for multiple documents to be generated at task run time, one for each row of the query recordset:6

The document is then made available to other steps in the task where it can be automatically delivered via email and/or saved to a location.

Using the Run Microsoft Word (Merge) Tool

From the task browser under Format tools drag and drop the Run Microsoft Word (Merge) icon onto the task planner. This will automatically open the tool for configuration.

General Tab

Within the General tab you provide an appropriate name and description for the step.

If you want to generate multiple documents from a TaskCentre query then a recordset will need to be selected. Each time the task processes a row from the query the data inserted into the merge fields will dynamically change, therefore producing a new document each time.

Tick the box Use a Recordset and use the drop down list to select a step that outputs a recordset.

Alternatively, the tick box can be left blank if you plan on using a data source from within MS Word to populate the merge fields.
1

Main Tab

The Main tab provides features for importing a Word document or template containing mail merge fields. TaskCentre data can be mapped to the fields from the task browser, alternatively the fields can be left un-mapped if the original data source of the mail merge fields is to be used.

2

Import – This button opens a file browser to enable a MS Word document or template to be loaded into the tool.

Export – This button opens a file browser where a save location can be selected if the document needs to be exported. This is used when changes are made to the document from within the tool by using the Edit button.

Edit – This button launches the MS word application to enable editing of the currently loaded document. Any changes made will overwrite the existing document that has been imported into the tool.

Field Mapping (table) – This area is used to map data to MS Word mail merge fields. A table displays the merge fields available for mapping in the Mail Merge Field column. TaskCentre data may be dragged from the task browser into Data Source Column to map data to the merge fields.

If the Data Source Column is left blank then no data will be mapped to a merge field, the document will utilise its own mail merge data source if one is present.

Capture1

Output document generation (sub-section) – 3 options are available here which define how many documents will be generated:

Capture2

  • One document – All the rows of data from the recordset are included in a single output document.
  • New document when column changes – This option is used in conjunction with the ‘Column’ drop down list. Multiple recordset rows are included in a document. A new document will be generated when a change in the contents of a column is detected. For example, if the column selected in the recordset is Company Name, each time a change of company name is detected a new document is produced.Capture3
  • New document for each row – A new document is generated for each row in a recordset

Document Properties Tab

This tab is used to specify the document properties. Ticking the box Override existing document properties enables editing of the initially greyed out fields. Text can manually be entered or by using TaskCentre data from the task browser, dynamically created at task run time.

5

Advanced Tab

This tab allows an override of the SQL statement that Word uses to select the mail merge data source. Ticking the box Use custom query string will enable the text field beneath it so that script can manually be entered.

To read about tasks which make use of this tool see:

Building a task that sends a MS Word document created by using merge fields