New to Telerik UI for WinForms? Download free 30-day trial

Document Variables

Document variables provide a mechanism to store information in the document. They also provide a convenient way to define more complicated field constructions (nested fields) and can be used as the backbone of master-detail mail merge.

Each RadDocument instance has a dictionary of variables exposed by the DocumentVariables property:


Telerik.WinForms.Documents.Model.DocumentVariableCollection variables = this.radRichTextEditor1.Document.DocumentVariables;

Dim variables As Telerik.WinForms.Documents.Model.DocumentVariableCollection = Me.radRichTextEditor1.Document.DocumentVariables

This collection maps string keys to object values (most often strings). The values of the fields most often are strings again and contain the text that will be inserted in the document when the variable is inserted in the document and is evaluated. Variables can be added to this collection in one of the following ways:

this.radRichTextEditor1.Document.DocumentVariables.Add("Name", "Andrew Fuller");
this.radRichTextEditor1.Document.DocumentVariables["Job"] = "Software Engineer";

Me.radRichTextEditor1.Document.DocumentVariables.Add("Name", "Andrew Fuller")
Me.radRichTextEditor1.Document.DocumentVariables("Job") = "Software Engineer"

This code will add two variables to the document – "Name", which will be evaluated to "Andrew Fuller" and "Job" with a value "Software Engineer". The second syntax can also be used to modify the value of a variable that has already been added to the collection.

Removing variables from the collection can be done like this:


this.radRichTextEditor1.Document.DocumentVariables.Remove("Name");

Me.radRichTextEditor1.Document.DocumentVariables.Remove("Name")

Document variables can be inserted in the document using DocumentVariableField. Here is an example how the above created Name variable can be inserted in the current document of the editor:

Telerik.WinForms.Documents.Model.DocumentVariableField docVariable = new Telerik.WinForms.Documents.Model.DocumentVariableField() { VariableName = "Name" };
this.radRichTextEditor1.InsertField(docVariable);

Dim docVariable As New Telerik.WinForms.Documents.Model.DocumentVariableField() With {.VariableName = "Name"}
Me.radRichTextEditor1.InsertField(docVariable)

In this article