DOCVARIABLE

  • 3 minutes to read

DOCVARIABLE

Mixed field

{ DOCVARIABLE "variable name" "argument1" "argument 2"... }

Inserts a string associated with a document variable.

The following members are used to obtain a DOCVARIABLE field value.

Overview

Member Description
Document.Variables DXRichEdit first searches this collection for a specified variable.
Document.CalculateDocumentVariable If variable is not found in the collection, the control raises this event so you can specify the value in code.

Example: How to Handle the CalculateDocumentVariable event to insert dynamic content

TIP

You can use the DOCVARIABLE field to insert formatted strings or content from the RichEditDocumentServer or SubDocument objects. Refer to the How to: Insert Dynamic Content topic for a code sample.

This code snippet demonstrates how to handle the RichEditControl.CalculateDocumentVariable event to insert dynamic content into the document. In this example, it is used to get weather report or geo coordinates from Google web service. A variable name specified in the DOCVARIABLE field indicates a choice between location and weather, while the location itself is specified by the field argument.

void eventHandler_CalculateDocumentVariable(object sender, CalculateDocumentVariableEventArgs e) {
    string location = e.Arguments[0].Value.ToString();

    Console.WriteLine(e.VariableName + " " + location);

    if ((location.Trim() == String.Empty) || (location.Contains("<"))) {
        e.Value = " ";
        e.Handled = true;
        return;
    }

    switch (e.VariableName) {
        //case "Weather":
        //    Conditions conditions = new Conditions();
        //    conditions = Weather.GetCurrentConditions(location);
        //    e.Value = String.Format("Forecast for {0}: \nConditions: {1}\nTemperature (C) :{2}\nHumidity: {3}\nWind: {4}\n",
        //        conditions.City, conditions.Condition, conditions.TempC, conditions.Humidity, conditions.Wind);
        //    break;
        case "Location":
            GeoLocation[] loc = GeoLocation.GeocodeAddress(location);
            e.Value = String.Format(" {0}\nLatitude: {1}\nLongitude: {2}\n",
                loc[0].Address, loc[0].Latitude.ToString(), loc[0].Longitude.ToString());
            break;
    }
    e.Handled = true;
}
NOTE

Set the event arguments' Value property to the DocVariableValue.Current value in the CalculateDocumentVariable event handler to save the initial DOCVARIABLE field value.

Use the FieldOptions.UpdateDocVariablesBeforeCopy, Document.UpdateDocVariablesBeforePrint or the FieldOptions.UpdateDocVariablesBeforePrint properties to specify whether to update the DOCVARIABLE fields before printing or copying a range to the clipboard.