Products Technologies Demo Docs Blog Support Company

Reporting: Merging MS Word Documents with DocVariables

TX Text Control supports all MS Word fields including the typical merge fields, content control fields, legacy form fields or special fields such as DOCVARIABLE. All of those fields can be found in the ApplicationField collection. For the most typical fields, FieldAdapters are available to create and handle MS Word compatible fields directly. The MailMerge class populates fields of type MERGEFIELD automatically with data from different data sources. In order to merge fields of type…

Reporting: Merging MS Word Documents with DocVariables

TX Text Control supports all MS Word fields including the typical merge fields, content control fields, legacy form fields or special fields such as DOCVARIABLE. All of those fields can be found in the ApplicationField collection.

For the most typical fields, FieldAdapters are available to create and handle MS Word compatible fields directly.

The MailMerge class populates fields of type MERGEFIELD automatically with data from different data sources. In order to merge fields of type DOCVARIABLE, those fields need to be converted first:

TXTextControl.LoadSettings ls = new TXTextControl.LoadSettings();
ls.ApplicationFieldFormat = TXTextControl.ApplicationFieldFormat.MSWord;

textControl1.Load("template.docx",
    TXTextControl.StreamType.WordprocessingML, ls);

// loop through all fields in all text parts
foreach (IFormattedText textPart in textControl1.TextParts)
{
    foreach (ApplicationField appField in textPart.ApplicationFields)
    {
        // convert all "DocVariable" fields to "MergeFields"
        if (appField.TypeName == "DOCVARIABLE")
        {
            MergeField newField = new MergeField();
            newField.Name = appField.Parameters[0];

            appField.Parameters = newField.ApplicationField.Parameters;
            appField.TypeName = "MERGEFIELD";
            appField.Text = newField.Name;
        }
    }
}

All ApplicationFields of type DOCVARIABLE are converted to fields of type MERGEFIELD by changing the TypeName property of the ApplicationField instance. The Parameters are copied by using a FieldAdapater for the type MERGEFIELD. This helps to keep the format consistent and compatible to MS Word.

At the end, the text of the field is set to the name of the field, as DOCVARIABLES have no visible text in MS Word.

Download the sample from GitHub and test it on your own.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

GitHub

Download and Fork This Sample on GitHub

We proudly host our sample code on github.com/TextControl.

Please fork and contribute.

Download ZIP

Open on GitHub

Open in Visual Studio

Requirements for this sample

  • Visual Studio 2012 or better
  • TX Text Control .NET for Windows Forms (trial sufficient)

Reporting

The Text Control Reporting Framework combines powerful reporting features with an easy-to-use, MS Word compatible word processor. Users can create documents and templates using ordinary Microsoft Word skills. The Reporting Framework is included in all .NET based TX Text Control products including ASP.NET, Windows Forms and WPF.

See Reporting products

Related Posts

ASP.NETWindows FormsWPF

Official TX Text Control .NET Sample Applications Are Now Hosted on GitHub

This article gives a quick overview of the new repositories, their structure and our plans for the future.


ReportingWindows FormsGitHub

Windows Forms: Printing Multiple Pages Per Sheet

This sample project implements the class MultipagePrintDocument that inherits from System.Drawing.Printing.PrintDocument to print multiple pages of a document per sheet. The constructor of…


ReportingWindows FormsGitHub

Inserting Watermark Images to All Pages Dynamically

This sample project shows how to create and insert a watermark image on all pages dynamically. Image objects have the Name property to store additional string information with an image. This…


ReportingWindows FormsGitHub

MailMerge: Conditional INCLUDETEXT Fields

In documents and reports, often sentences or an appendix are added only under specific conditions. The Text Control reporting engine MailMerge provides the concept of INCLUDETEXT fields to include…


ReportingGitHubSample

Mail Merge: Suppress Lines with Empty Merge Fields

Consider an address block with several merge fields in a mail merge letter. Some fields might be empty such as the firstname and name. If no data is available for those fields, the complete line…