Skip to main content
A newer version of this page is available. .

Fields

  • 7 minutes to read

This topic consists the following sections:

Overview

The Field is a set of codes that instructs the RichEditControl to insert text or graphics into a document automatically. The field code syntax is illustrated and described below.

DocumentFields_Structure

  • Field Name - the name of the current field. Refer to the Field Codes section for a list of all available fields.
  • Property - instruction/variable used in a particular field. This is an optional element.
  • Switch - an additional parameter that offers more information. This is an optional element. Refer to the Format Switches section for a list of supported format switches.

A field in the document consists of two ranges - the Field.CodeRange and the Field.ResultRange. Use the Field.Range property to obtain the total range the field occupies.

The RichEditControl supports the following field types:

  1. Non-MailMerge

    Regular fields used to insert a simple data, such as DATE, TIME or PAGE. The Non-MailMerge field results are shown after the update.

  2. MailMerge

    Fields used in the Mail Merge process. They get a value only if there is a mail merge data source bound to the RichEditControl. In the resulting document, MailMerge fields are evaluated and substituted with the resulting values.

  3. Mixed

    Fields which do not require a data source to get its value, such as INCLUDEPICTURE and CREATEDATE. They are substituted with the resulting values during mail merge.

  4. Formula

    The = (Formula) field is a code that uses mathematical formulas and numeric field values to calculate a number. The formula field syntax looks as follows:

    {={NUMERIC FIELD} Operator {NUMERIC FIELD}}

    The RichEditControl supports the following operators in the formula field:

    • Addition (+)
    • Subtraction (-)
    • Multiplication (*)
    • Division (/)
    • Powers and Roots (^)
    • Equal to (=)
    • Less than (<)
    • Less than or equal to (<=)
    • Greater than (>)
    • Greater than or equal to (>=)
    • Not equal to (<>)

    You can change the field’s numeric format. Refer to the Numeric Format Switch topic for a list of available format switches.

Insert and Modify Fields

Members from the table below allow you to insert and modify the field programmatically.

Member Description
SubDocument.BeginUpdate Opens the document for editing.
BeginUpdateDocument() Opens the document related to the current position for editing.
FieldCollection.Create Creates a new Field object.
SubDocument.Fields Provides access to the collection of fields in the current document.
Field.Range Gets the document range occupied by the field.
Field.CodeRange Gets the range containing the field codes.
Field.ResultRange Gets the range containing the field result.
SubDocument.InsertText Inserts the specified text at the specified position.
SubDocument.EndUpdate Finalizes the modification.
DocumentPosition caretPosition = document.CaretPosition;
SubDocument currentDocument = caretPosition.BeginUpdateDocument();

//Create a DATE field at the caret position
currentDocument.Fields.Create(caretPosition, "DATE");
currentDocument.EndUpdate();

for (int i = 0; i < currentDocument.Fields.Count; i++)
{
    string fieldCode = document.GetText(currentDocument.Fields[i].CodeRange);
    if (fieldCode == "DATE")
    {
        //Retrieve the range obtained by the field code
        DocumentPosition position = currentDocument.Fields[i].CodeRange.End;

        //Insert the format switch to the end of the field code range
        currentDocument.InsertText(position, @"\@ ""M/d/yyyy h:mm am/pm""");
    }
}

//Update all document fields
currentDocument.Fields.Update();

Tip

The header and footer and the text box in the RichEditControl document do not belong to the main document body. Retrieve related SubDocument object to manage the corresponding fields. Use the following API to complete the task:

Update Fields

Most fields are updated automatically when the document is saved or printed, or during the mail merge operation. Use the Field.Update method to update the field on demand. Specify the DocumentImporterOptions.UpdateField property to set what document fields should be updated automatically when loading the document. Use the richEditControl.Options.Import.DocumentFormat.UpdateField notation (where DocumentFormat is one of the RichTextEditDocumentImportOptions properties) to access this property.

Set the field’s Field.Locked property to true to prevent a specific field from being updated. Use the FieldOptions.UpdateLockedFields option to allow updating locked fields.

The DOCVARIABLE field is not updated during mail merge or document insertion. Handle the RichEditControl.CalculateDocumentVariable event to update the DOCVARIABLE fields’ values.

The RichEditControl.CalculateDocumentVariable event does not occur for a locked DOCVARIABLE field, and the field value remains unchanged. Set the FieldOptions.UpdateLockedFields option to the UpdateLockedFields.DocVariableOnly value to update DOCVARIABLE fields.

Tip

A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=E3099.

Field Options

Use the Field.ShowCodes property to change the target field’s display mode. You can also use ShowAllFieldCodesCommand or ShowAllFieldResultsCommand commands to change the display mode of all document fields.

This code snippet displays field codes for all fields in the main document body.

The fields containing in the header or footer belong to a different FieldCollection. Use the Section.BeginUpdateHeader- Section.EndUpdateHeader or Section.BeginUpdateFooter - Section.EndUpdateFooter paired methods to retrieve the header or footer document.

document.LoadDocument("MailMergeSimple.docx", DevExpress.XtraRichEdit.DocumentFormat.OpenXml);
for (int i = 0; i < document.Fields.Count; i++)
{
    document.Fields[i].ShowCodes = true;
}

Use the RichEditControlOptionsBase.Fields to specify how RichEditControl should process and display document fields, as shown in the code snippet below:


FieldOptions fieldOptions = richEditControl.Options.Fields;

fieldOptions.HighlightMode = FieldsHighlightMode.Always;
fieldOptions.HighlightColor = System.Drawing.Color.LightSalmon;

fieldOptions.UseCurrentCultureDateTimeFormat = true;
fieldOptions.ThrowExceptionOnInvalidFormatSwitch = true;
fieldOptions.UpdateFieldsInTextBoxes = true;

Set the DocumentCapabilitiesOptions.Fields property to DocumentCapability.Hidden or DocumentCapability.Disabled to restrict inserting fields to the document.

Operate Fields in the User Interface

To simplify many common tasks when working with fields, the Rich Editor UI provides the following commands and shortcuts:

Task UI Command / Shortcut Key
Insert a field Press CTRL+F9.
Show field codes Press ALT+F9 or click the Show All Field Codes button in the Mail Merge Ribbon page or Bar group.
Update field values Select a range containing fields and press F9.
Show spaces and formatting marks Press CTRL+SHIFT+8.

End users can update or toggle the code of the document field from the context menu or by using the Mail Merge ribbon tab. Refer to the How to: Create the RichEditControl with a Ribbon UI topic for details on how to provide a Ribbon UI for the RichEditControl.

Fields-Code

MERGEFIELD Display Modes

When the Show All Field Results button is clicked (or the ShowAllFieldResultsCommand is executed), the MERGEFIELD field is shown as a placeholder:

Fields-Placeholder

End users can click View Merged Data to display the MERGEFIELD code result:

Fields-Result

Set the RichEditMailMergeOptions.ViewMergedData to true or execute the ToggleViewMergedDataCommand command to achieve this task programmatically.

See Also