Page tree
Skip to end of metadata
Go to start of metadata

Creating a Template

OfficeWriter's WordTemplate object opens a WordWriter template file, populates it with data from one or more data sources, and generates a new Word file. A WordWriter template is a file created in Microsoft Word that contains merge fields. A merge field displays a data source field name (for example, a database column name) where a data source value will be inserted. Merge fields are bound to a data source in OfficeWriter code. When you run the code, OfficeWriter replaces the merge fields with v

Creating Merge Fields

A WordWriter template is a Microsoft Word file that contains merge fields. A merge field displays a data source field (for example, a database column name). A merge field is created in Microsoft Word and bound in code to a data source. The data source may be an array, a DataSet, a DataTable, or a DataReader. When you run the code, WordWriter populates the merge fields with data source values.

Formatting Merge Fields

When you create a merge field in Microsoft Word (see Creating a WordWriter Template), you can apply formatting to the field. For example, you can set a merge field's format to "Uppercase" to display the field's value in uppercase letters. WordWriter supports most text, date, and time formats available in Microsoft Word.

Inserting an Image

WordWriter allows you to insert jpg, gif, bmp, and png images in merge fields. You can insert images repeat block and main document merge fields. There are two ways to insert an image into a merge field:

Adding Formulas to a Document

Beginning in WordWriter version 4.1, WordTemplate now has the ability to evaluate a certain number of formulas on data fields that are brought into a template.

Inserting an Embedded Document

WordTemplate can insert certain types of documents into docx files by adding the "document(format)" modifier to the merge field, where format is one of the following:

Creating a Grouped and Nested Document

Beginning in version 4.5, the WordTemplate object includes support for nested bookmarks with Office Open XML documents (.docx, .docm, .dotx, and .dotm) in WordWriter Enterprise Edition. Nested bookmarks can be used to format and display hierarchical data.

Using a Database as a Data Source

You can use a database table as a data source for a WordWriter template by passing the SetDataSource method a DataTable, a DataSet, an SqlDataReader, OleDbDataReader or an AdomdDataReader. If the specified data source contains more than one row, SetDataSource will use the first row of the first table as the data source. To import multiple rows from data sources use a repeat block.

Importing from Multiple Data Sources

The main document may include multiples sets of merge fields, where each set of fields binds to a different data source. A SetDataSource call is required for each set of main document merge fields. Only one row of data can be imported to each set of main document merge fields. When you include more than one set of merge fields in the main document, use any of the following SetDataSource methods to set data sources:

Using an Array as a Data Source

The following example generates a Word file from a template that contains the merge fields ProductName, CompanyName, and URL. The WordWriter code gets the values for these fields from an array of objects.

Importing Multiple Rows

Use a repeat block to import multiple rows from a data source. A repeat block is a fragment in the template document - defined by a Word bookmark - that contains merge fields and that will be repeated for each row in a data source. To import multiple rows from a single data source, create a repeat block in the template and, in the WordWriter code, call SetRepeatBlock to bind the repeat block to a data source.

WordTemplate Output Options

After populating your WordWriter template from a data source, call Save to either save the new Word file on the server, or stream it to the browser. WordWriter can:

  • No labels