Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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 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. Press ALT + F9 to hide the field codes again.

The template is now ready for NEXT fields.

Changes to the code for NEXT fields

By default, NEXT fields will be ignored by WordWriter. To enable NEXT fields, set the WordTemplate.EnableNEXTFields property. This property must be set before WordTemplate.Open is called.

Code Block

WT.EnableNEXTFields = true;

Final Code

Code Block

//Instantiate a new WordTemplate object
WordTemplate WT = new WordTemplate();

//NEXT fields will be ignored by default
//This must be set before Open() is called
WT.EnableNEXTFields = true;

//Open the template file
WT.Open(Page.MapPath("//templates//Business_Label_template.docx"));

//Get the order info datatable using GenericParser
DataTable dtBusinessLabels = GetCSVData("//data//BusinessLabelTutorialData.csv");

//Set the data sources to import a single row of data for each source
WT.SetMailMerge(dtBusinessLabels);

//Process to import the data to the template
WT.Process();

WT.Save(Page.Response, "Business_Labels_Output.docx", false);

Run the code. The business labels are now populated throughout the page, only moving to the second page after the first page is filled.

Image Added

Downloads