Importing Fields from Microsoft Word

TX Text Control supports field formats of other applications such as Microsoft Word. The ApplicationField class provides an interface to retrieve and change the data and parameters of such fields. Currently supported formats are the Microsoft Word Field Format, the format of Microsoft Word Form Fields and the field format of the Heiler HighEdit component. Because these formats are not compatible, the user must specify the format when a document is loaded. The following code imports all MergeField and PrintDate fields contained in a RTF document that has been created with Microsoft Word:

LoadSettings ls = new LoadSettings();
ls.ApplicationFieldFormat = ApplicationFieldFormat.MSWord;
ls.ApplicationFieldTypeNames = new string[] { "MERGEFIELD", "PRINTDATE" };
textControl1.Load("c:\\test.rtf", TXTextControl.StreamType.RichTextFormat, ls);
[Visual Basic]
Dim ls As New LoadSettings
ls.ApplicationFieldFormat = ApplicationFieldFormat.MSWord
ls.ApplicationFieldTypeNames = New String() {"MERGEFIELD", "PRINTDATE"}
TextControl1.Load("c:\test.rtf", TXTextControl.StreamType.RichTextFormat, ls)

After the document has been loaded all MergeField and PrintDate fields are available through the TextControl.ApplicationFields collection. The collection contains objects of the type ApplicationField. The ApplicationField class has a TypeName property specifying the field's type and a Parameters property specifying the field's parameters as an array of strings. The following is the format of a MergeField defined through Microsoft Word:

MERGEFIELD FieldName [Switches]

For example: MERGEFIELD Address \* Upper \v

For further information about the switches and it's meaning, please refer to Microsoft's Field Reference:

In this example the FieldName is Address and there are two switches specifying some formatting options. The ApplicationField.Parameters array then contains three elements, the first of which is the FieldName and the others are the switches.

The following code iterates through all fields imported with the example above and sets the visible text of the address field:

foreach (ApplicationField appField in textControl1.ApplicationFields) {
    if (appField.TypeName == "MERGEFIELD" && appField.Parameters[0] == "Address") {
        appField.Text = "Bakerstreet, London";
[Visual Basic]
For Each appField As ApplicationField In TextControl1.ApplicationFields
    If appField.TypeName = "MERGEFIELD" And appField.Parameters(0) = "Address" Then
        appField.Text = "Bakerstreet, London"
    End If

To add to or remove elements from the Parameters string array a new array must be constructed because .NET arrays have a fixed size. The following code looks for a \v switch and removes it from the parameters array. First a new array is created and the remaining strings that can be in front or behind the searched string are copied:

foreach (ApplicationField appField in textControl1.ApplicationFields) {
  string[] params = appField.Parameters;
  int index = Array.IndexOf(params, "\\v");
  if (index >= 0) {
    string[] newParams = new string[params.Length-1];
    if (index > 0) {
      Array.Copy(params, newParams, index);
    if (params.Length-index-1 > 0) {
      Array.Copy(params, index+1, newParams, index, params.Length-index-1);
    appField.Parameters = newParams;
[Visual Basic]
For Each appField As TXTextControl.ApplicationField In TextControl1.ApplicationFields
  Dim params() As String = appField.Parameters
  Dim index As Integer = Array.IndexOf(params, "\v")

  If index >= 0 Then
    Dim newParams(params.Length - 2) As String
    If index > 0 Then
      Array.Copy(params, newParams, index)
    End If
    If params.Length - index - 1 > 0 Then
      Array.Copy(params, index + 1, newParams, index, params.Length - index - 1)
    End If

    appField.Parameters = newParams
  End If

TX Text Control also supports importing and exporting of Microsoft Word form fields. These fields can be imported specifying FORMCHECKBOX, FORMDROPDOWN or FORMTEXTBOX with the LoadSettings.ApplicationFieldTypeNames property. Microsoft Word form fields also have parameters, that can be edited in Microsoft Word with the Form Field Options dialog box. TX Text Control provides these parameters through the ApplicationField.Parameters property using the XML syntax of the Office Open XML Format.

To learn more about the Office Open XML Format, please refer to the official ECMA standard:

MS Word implements another feature to support individual controls like form elements in templates or forms: Content Controls. The following controls are available since MS Word 2007: Rich Text, Text, Picture, Combo Box, Drop-Down List and Check Box.

Just like for the merge and form fields, TX Text Control imports these fields as ApplicationFields available in the ApplicationField collection. TX Text Control provides parameters through the ApplicationField.Parameters property using the XML syntax of the Office Open XML Format. The content controls have the ApplicationField.TypeName SDTBLOCK. The type of the control is defined in the XML of the ApplicationField.Parameters property. The w:val parameter of the w:tag defines the type. The following types are available:

Content Control Description
Rich Text Control Text content that contains formatting.
Plain Text Control Limited to content that cannot contain any formatting, only plain text.
picture Fills the content control with a single picture.
Combo Box Contains a list that can be edited directly.
Check Box Provides a check box users can check and uncheck. The checked value is stored in the XML data.
dropDownList Contains a list of restricted choices defined by the template author when the user activates the drop-down box.

Please refer to the official Microsoft Office documentation to learn more about the various content controls: