Example T830501
Visible to All Users

Word Processing Document API - How to Use DOCVARIABLE Fields in a Document

This example illustrates the use of a DOCVARIABLE field to provide additional information which is dependent on the value of a merged field. This technique is implemented so each merged document contains a weather report for a location that corresponds to the current data record.

Note

We do not provide code for retrieving weather information. You can implement a custom weather information provider.

Implementation Details

[!Important]
The Universal Subscription or an additional Office File API Subscription is required to use this example in production code. For pricing information, please refer to the DevExpress Subscription page.

A MERGEFIELD field defines a location. The field is included as an argument in the DOCVARIABLE field. When the DOCVARIABLE field is updated, the Document.CalculateDocumentVariable event is triggered. A code within the event handler obtains the weather information. The e.VariableName property gets the name of the variable in the field, the e.Arguments property gets the location, and the e.Value property returns the calculated result.

The MailMergeRecordStarted event is handled to insert a hidden text that indicates when the document is created.

Files to Review

Documentation

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

Program.cs(vb)
C#
using DevExpress.XtraRichEdit; using DevExpress.XtraRichEdit.API.Native; using System; using System.Collections.Generic; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WordProcessingFileAPI_CalcDocumentVariable { class Program { static void Main(string[] args) { RichEditDocumentServer wordProcessor = new RichEditDocumentServer(); wordProcessor.LoadDocument("Docs\\invitation.docx", DocumentFormat.OpenXml); Document document = wordProcessor.Document; //Lock the first field to prevent updates document.Fields[0].Locked = true; // Handle the CalculateDocumentVariable event document.CalculateDocumentVariable += Document_CalculateDocumentVariable; // Adjust mail-merge options MailMergeOptions myMergeOptions = document.CreateMailMergeOptions(); myMergeOptions.MergeMode = MergeMode.NewSection; myMergeOptions.DataSource = new SampleData(); // Handle mail-merge events wordProcessor.MailMergeRecordStarted += wordProcessor_MailMergeRecordStarted; wordProcessor.MailMergeRecordFinished += wordProcessor_MailMergeRecordFinished; // Mail-merge the document document.MailMerge(myMergeOptions, "Result.docx", DocumentFormat.OpenXml); var p = new Process(); p.StartInfo = new ProcessStartInfo(@"Result.docx") { UseShellExecute = true }; p.Start(); } private static void Document_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; } private static void wordProcessor_MailMergeRecordStarted(object sender, MailMergeRecordStartedEventArgs e) { DocumentRange insertedRange = e.RecordDocument.InsertText(e.RecordDocument.Range.Start, String.Format("Created on {0:G}\n\n", DateTime.Now)); CharacterProperties cp = e.RecordDocument.BeginUpdateCharacters(insertedRange); cp.FontSize = 8; cp.ForeColor = Color.Red; e.RecordDocument.EndUpdateCharacters(cp); } private static void wordProcessor_MailMergeRecordFinished(object sender, MailMergeRecordFinishedEventArgs e) { e.RecordDocument.AppendDocumentContent("Docs\\bungalow.docx", DocumentFormat.OpenXml); } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.