Table of Contents
WordTemplate object is used for template-driven document generation. This object opens a WordWriter template file, populates it with data from a specified data source, and generates a new Word document. The
WordTemplate object provides the ability to import multiple rows of data by repeating sections of a document for each row, or using the entire document as a repeatable section.
This tutorial covers how to use
WordTemplate.SetMailMerge to use the entire document as a repeatable section and then how to use NEXT fields to populate a sheet of business labels, over multiple pages.
Business Label Template
The template file looks like this:
There is a table with fields for an employee's FirstName, MI, LastName, Phone, Email, Street, City, State, and ZipCode. These fields are repeated for an entire page in the document. The goal is to populate all of the business labels, but spill onto multiple pages as necessary.
Unlike with repeat blocks that are populated with
WordTemplate.SetRepeatBlock, which require bookmarks to determine what part of the document is repeated,
WordTemplate.SetMailMerge treats the entire document as a repeat block, so it is not necessary to add bookmarks when using
Adding a WordWriter Reference in Visual Studio
To create a .NET project and add a reference to the WordWriter library:
- Open Visual Studio and create a .NET project.
- Add a reference to the SoftArtisans.OfficeWriter.WordWriter.dll
SoftArtisans.OfficeWriter.WordWriter.dllis located under Program Files > SoftArtisans > OfficeWriter > dotnet > bin.
Writing the Code
This part of the tutorial will cover how to set up the WordTemplate code to bind the data to the template. This assumes a basic knowledge of
WordTemplate and how to bind data to Word templates.
1. Include the SoftArtisans.OfficeWriter.WordWriter namespace in the code behind
2. Create a new
WordTemplate object and open the template file.
3. Get the employee data for the business labels.
This call is to a helper method
GetCSVData that parses the CSV files and returns a
DataTable with the values.
WordTemplate.SetMailMerge to bind the employee data to the template.
SetMailMerge treats the entire document as a repeat block. This means that the merge fields in the document will need to match the column names of the data source. If there are any stray merge fields in the document, WordWriter will throw an error.
5. Process and save the template file.
The completed code should look like this:
Run the code. You will notice that each employee has a page of business labels with that particular employee's data.
This is the intended
SetMailMerge behavior, as the entire document was repeated for each row of data. In the next section we will incorporate next fields so that only one business label is made for each employee.
Adding NEXT fields
The NEXT fields change how WordWriter imports multiple rows of data. In regular repeat blocks, an entire portion of the document will be repeated for every row of data, or in the case of
SetMailMerge, the entire document will be repeated. With NEXT fields, WordWriter will first look to see if it can populate a merge field that is preceded by a NEXT field before repeating the section.
This will allow us to populate through an entire page of business labels before moving on to a second page.
NEXT fields in the template
1. Place the cursor in the topmost right table cell. This is where the NEXT field will go.
2. Go to Insert > Quick Parts and select Field from the drop-down. Select Next from the list of available fields. Click OK to add the NEXT field.
3. Repeat this process for each business label on the page, except for the first business label. When you are done, press ALT + F9 to show all the NEXT fields to confirm.
4. Press ALT + F9 to hide the field codes again. The template is now ready for NEXT fields.
Changes to the code for NEXT fields
Run the code. The business labels are now populated throughout the page, only moving to the second page after the first page is filled.
You can download the code for the Basic WordWriter Tutorials as a Visual Studio solution, which includes the Business Labels tutorial.