Implementing a MERGEFIELD Class

The MS Word MergeField is the most commonly used field in MS Word and RTF documents. It enables template designers to add additional information to a text field, such as a specific text that should be displayed before or after the field text or the format of the text.

To handle these fields and it's functionality, this sample uses the DocumentServer.Fields.FieldAdapter implemented in the TXTextControl.DocumentServer. These adapters can be used to modify the TextControl.ApplicationFields directly.

After starting the sample, click on File / Load to load a sample document, which contains several merge fields. Set the input position inside an exsiting field, for sample <<COMPANY>>.

Open the implemented merge field dialog by choosing Edit... from the MergeField main menu.

Using this dialog, it is possible to manipulate all properties of the MergeField instance. Add some text that should be inserted after the field by checking the checkbox Text to be inserted after. Type some text in the appropriate text box and close the dialog by clicking OK.

Open the customer list view form by clicking Merge fields... from the Merge menu entry. Choose a customer by clicking on a specific row and click Merge to merge all merge fields with the database content.

In this process, the ApplicationFieldCollection is used to loop through all inserted fields to replace the text. The following code shows this loop:

DataRow drCustomer = m_customers.Tables[0].Rows[listBox1.SelectedIndex];

foreach (TXTextControl.ApplicationField curField in _tx.ApplicationFields)
	if (curField.TypeName != "MERGEFIELD")

	MergeField mfCurField = new MergeField(curField);

		mfCurField.Text = drCustomer[mfCurField.Name].ToString();
[Visual Basic]
Dim drCustomer As DataRow = m_customers.Tables(0).Rows(listBox1.SelectedIndex)

For Each curField As TXTextControl.ApplicationField In _tx.ApplicationFields
	If curField.TypeName <> "MERGEFIELD" Then
	End If

	Dim mfCurField As New MergeField(curField)

	If drCustomer.Table.Columns.Contains(mfCurField.Name) Then
		mfCurField.Text = drCustomer(mfCurField.Name).ToString()
	End If

<< Previous Next >>