The main document is any part of the template that is not within a repeat block. SetDataSource sets a single-row data source for merge fields in the main document. If you pass SetDataSource a ResultSet that contains more than one row, WordWriter will import the first row to the template. Multiple row data sources can be imported to repeat blocks only, not to main document merge fields.
To create a repeat block:
- In Microsoft Word, create a text fragment, a list, or a table row that contains merge fields.
- Select the text, list, or table row that you want to define as a repeat block.
- Open the Insert menu and select Bookmark... to open the Bookmark dialog.
- Enter a bookmark name and click Add.
In Microsoft Word, bookmarks are not marked by default. To see which document fragments are bookmarks:
- Open the Tools menu and select Options...
- Select the View tab.
- Under Show, check Bookmarks. Bookmarks will be marked by grey brackets.
Whether the template will bind to one data source or several, merge fields are not required in the main document. To import multiple rows from a single data source, use a repeat block only, and do not include any merge fields in the main document. The repeat block can span all the content in the document. For example, by defining a repeat block with a bookmark that spans an entire page, and setting a multiple-row data source, you can use WordWriter to create multiple form letters:
Here is some example code that demonstrates the usage of SetRepeatBlock with a DataTable that has multiple rows. This code inserts data into merge fields "FirstName" and "LastName" enclosed in the repeat block named "BookMarkName".
Code Sample: Mail Merge using Repeat Blocks
The following code sample demonstrates the use of SetRepeatBlock to emulate mail merge behavior. The repeat block spans an entire page within the document. This causes a new page to be created for each recipient in the database: