DOCVARIABLE
- 3 minutes to read
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.
Member | Description |
---|---|
Document.Variables | RichEditControl 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: Handle the CalculateDocumentVariable Event to Insert Formatted String
Tip
You can handle the CalculateDocumentVariable event to insert content from the RichEditDocumentServer, SubDocument or DocumentRange object. Refer to the How to: Insert Dynamic Content topic for more information.
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 conditions. 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) {
if (e.Arguments.Count > 0) {
string location = e.Arguments[0].Value.ToString();
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("Weather for {0}: \nConditions: {1}\nTemperature (C) :{2}\nHumidity: {3}\nWind: {4}\n",
location, conditions.Condition, conditions.TempC, conditions.Humidity, conditions.Wind);
break;
case "LOCATION":
if (location == "DO NOT CHANGE!") e.Value = DocVariableValue.Current;
break;
default:
e.Value = "LOCKED FIELD UPDATED";
break;
}
}
else {
e.Value = "LOCKED FIELD UPDATED";
}
e.Handled = true;
}
Note
Set the event arguments’ CalculateDocumentVariableEventArgs.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.