|
WordTemplate in Depth
> Merge Field Syntax
Merge Field Syntax
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.
A template may contain multiple sets of merge fields. Each set of fields
binds to a single data source.
Implicitly or explicitly, a merge field always specifies both a data source and
a column. For example, in the merge field:
<<Products.ProductID>>
"Products" is the data source, and "ProductID" is the column. And the
merge field:
<<ProductID>>
Means the "ProductId" column from the first data source defined in the WordWriter code.
Both the data source and the column can be specified by either name or number:
<<Products.ProductID>>
|
|
The "ProductID" column of the data source "Products." |
<<#2.FirstName>>
|
|
The "FirstName" column of the second data source
defined in the WordWriter code. |
<<Employee.#3>>
|
|
The third column of the "Employee" data source. |
<<#2.#3>>
|
|
The third column of the second data source defined
in the WordWriter code. |
Data source and field numbers are 1-based. If WordWriter encounters
<<#0[.field]>> or <<[DataSource.]#0>>,
an error will occur.
The default data source for a main document merge field is the first data
source set in the WordWriter code (by the method SetDataSource).
The default data source for a merge field within a repeat block
is the data source set for the repeat block (by the method
SetRepeatBlock, which
specifies the repeat block by bookmark name). You can include main
document merge fields within a repeat block, however, these fields will not be
repeated when the template is populated (only one row of data can be
imported to main document merge fields). The data source for a repeat block is
only valid within the repeat block; you cannot include a repeat block merge field
outside of its bookmark-defined repeat block.
The Main Document and Repeat Blocks
A repeat block is a text fragment, a list, or a table row - defined by a Word bookmark
- that contains merge fields and that will be repeated for each row returned from the
database. The main document is any part of the template that is not within a repeat block.
You can assign one data source to each repeat block, and multiple data sources to the
main document. Only one row of data can be imported to each set on main document merge
fields. |
Merge Field Names

Follow these rules when naming merge fields:
- Data source and column names must not include Unicode characters.
- Data source and column names must begin with a letter (A-Z, a-z).
- Data source and column names may include the following characters only:
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890_
Note: If you assigned a different data source separator, you can also use a "."
in your data source and column names.
- Spaces are not allowed anywhere in a merge field except
within a column name in brackets (for example,
<<Products.[Product Category]>>.
To include spaces in the data source column name, use this format:
<<DataSource.[Column Name]>>
For example:
<<Employees.[Street Address]>>
The [Column Name] format allows you to match any SQL column name exactly. Legal
characters within the brackes are:
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890_ @$#
Using Short Merge Fields

WordWriter supports several full-length and short merge field forms.
When you use any of the long merge field formats, you explicitly include
both the data source name or number and the column name or number.
Short formats do not specify both data source and column explicitly; one or
the other is omitted.
How WordWriter understands a short merge field depends on whether or not
the marker contains a $.
WordWriter understands <<$Employee>>
as the first column of the data source "Employee."
If you omit the $, WordWriter will
read "Employee" as a column from the first data source defined in the WordWriter code. So:
<<$Employee>> means the first column
in the data source "Employee."
<<Employee>> means the "Employee" column
in the first data source defined in the WordWriter code.
Examples
| Form |
Example |
<<#ColumnNumber>> |
<<#7>>
Seventh column of the first
data source defined in the WordWriter code.
|
<<$#DataSourceNumber>> |
<<$#7>>
First column of the seventh data source
defined in the WordWriter code.
|
<<ColumnName>> |
<<OrderId>>
"OrderId" column from the first data source
defined in the WordWriter code.
|
<<$DataSourceName>> |
<<$Orders>>
First column in the data source
"Orders."
|
Long Merge Fields

When you use any of the long merge field formats, you explicitly include
both the data source name or number and the column name or number.
Using the long formats may prevent confusion when writing the WordWriter
code, since you must be able to distinguish between data source and
column to set a data source in code.
Examples
| Form |
Example |
<<DataSourceName.ColumnName>> |
<<Employees.FirstName>>
"FirstName" column in the "Employees" data source. |
<<DataSourceName.#ColumnNumber>> |
<<Products.#3>>
Third column of the data source "Products." |
<<#DataSourceNumber.ColumnName>> |
<<#2.ProductId>>
"ProductId" column of the second data source defined
in the WordWriter code. |
<<#DataSourceNumber.#ColumnNumber>> |
<<#3.#2>>
Second column of the third data source defined in the
WordWriter code. |
<<DataSourceName.[Column Name]>> |
<<Employees.[Street Address]>>
"Street Address" column of the data source "Employees." |
Merge Field Modifiers

An WordWriter merge field can include modifiers. Modifiers are
included in parenthesis at the end of the merge field. For example:
<<Products.ProductID(Lowercase)>> If a merge field includes
more than one modifier, the modifiers should be separated by commas.
Do not include spaces between modifiers.
WordWriter supports the following merge field modifiers:
Fieldname | Use the Fieldname
modifier to insert database column headings
in a document. For example, in place of the merge field
<<RecordSet.#2(fieldname)>>, WordWriter will insert the
name of the second column in the RecordSet. |
Uppercase | If a merge field includes the
Uppercase modifier, all text values
imported to the merge field will be displayed in uppercase.
If a merge field includes both the
Uppercase and Lowercase
modifiers, the last will be applied. |
Lowercase | If a merge field includes the
Lowercase modifier, all text values
imported to the merge field will be displayed in lowercase.
If a merge field includes both the
Uppercase and Lowercase
modifiers, the last will be applied. |
Optional | By default, if a merge field's data source name or column name
is not bound to a data source in code, an error will occur. If a
merge field contains the Optional modifier, and the merge field is
not bound by name to a data source, WordWriter will discard the merge field
in the generated spreadsheet and will not throw an error.
This modifier makes both a merge field's data source and column
optional. For example, for this merge field:
<<Orders.OrderID(Optional)
If either the data source "Orders," or the column "OrderId" does not
exist, the merge field will be removed without error.
Note:
- If a merge field contains a data source number and/or
field number (for example
<<#7.#3) and is not bound to
a data source in script, the merge field will be removed without
error whether the "Optional" modifier is present or not.
- If the flag
RemoveExtraDataMarkers Method is set to true,
all merge fields that do not bind to data sources
will be removed without error.
|

Copyright 2007 © SoftArtisans, Inc. All Rights Reserved.
|