# Reporting: Merging MS Word Documents with DocVariables

> TX Text Control can merge MS Word documents containing DOCVARIABLE fields by converting them to MERGEFIELD types. The code iterates through all ApplicationFields, changes the TypeName from DOCVARIABLE to MERGEFIELD, and copies parameters using a FieldAdapter for format consistency.

- **Author:** Bjoern Meyer
- **Published:** 2015-06-10
- **Modified:** 2026-07-17
- **Description:** TX Text Control can merge MS Word documents containing DOCVARIABLE fields by converting them to MERGEFIELD types. The code iterates through all ApplicationFields, changes the TypeName from DOCVARIABLE to MERGEFIELD, and copies parameters using a FieldAdapter for format consistency.
- **2 min read** (269 words)
- **Tags:**
  - GitHub
  - Reporting
  - Sample
  - Windows Forms
- **Web URL:** https://www.textcontrol.com/blog/2015/06/10/reporting-merging-ms-word-documents-with-docvariables/
- **LLMs URL:** https://www.textcontrol.com/blog/2015/06/10/reporting-merging-ms-word-documents-with-docvariables/llms.txt
- **LLMs-Full URL:** https://www.textcontrol.com/blog/2015/06/10/reporting-merging-ms-word-documents-with-docvariables/llms-full.txt
- **GitHub Repository:** https://github.com/TextControl/TextControl.MailMerge.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](https://docs.textcontrol.com/textcontrol/windows-forms/ref.txtextcontrol.applicationfield.class.htm) collection.

For the most typical fields, [FieldAdapters](https://docs.textcontrol.com/textcontrol/windows-forms/ref.txtextcontrol.documentserver.fields.fieldadapter.class.htm) are available to create and handle MS Word compatible fields directly.

The [MailMerge](https://docs.textcontrol.com/textcontrol/windows-forms/ref.txtextcontrol.documentserver.mailmerge.class.htm) 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.

---

## About Bjoern Meyer

As CEO, Bjoern is the visionary behind our strategic direction and business development, bridging the gap between our customers and engineering teams. His deep passion for coding and web technologies drives the creation of innovative products. If you're at a tech conference, be sure to stop by our booth - you'll most likely meet Bjoern in person. With an advanced graduate degree (Dipl. Inf.) in Computer Science, specializing in AI, from the University of Bremen, Bjoern brings significant expertise to his role. In his spare time, Bjoern enjoys running, paragliding, mountain biking, and playing the piano.

- [LinkedIn](https://www.linkedin.com/in/bjoernmeyer/)
- [X](https://x.com/txbjoern)
- [GitHub](https://github.com/bjoerntx)

---

## Related Posts

- [Official TX Text Control .NET Sample Applications Are Now Hosted on GitHub](https://www.textcontrol.com/blog/2023/01/08/official-tx-text-control-net-sample-applications-are-now-hosted-on-github/llms.txt)
- [Windows Forms: Printing Multiple Pages Per Sheet](https://www.textcontrol.com/blog/2015/07/24/windows-forms-printing-multiple-pages-per-sheet/llms.txt)
- [Inserting Watermark Images to All Pages Dynamically](https://www.textcontrol.com/blog/2015/07/17/inserting-watermark-images-to-all-pages-dynamically/llms.txt)
- [MailMerge: Conditional INCLUDETEXT Fields](https://www.textcontrol.com/blog/2015/01/08/mailmerge-conditional-includetext-fields/llms.txt)
- [Mail Merge: Suppress Lines with Empty Merge Fields](https://www.textcontrol.com/blog/2014/08/01/mail-merge-suppress-lines-with-empty-merge-fields/llms.txt)
- [Change Table Width to Fit Document Width - Automatically](https://www.textcontrol.com/blog/2005/02/03/change-table-width-to-fit-document-width-automatically/llms.txt)
- [TX Text Control 32.0 Has Been Released](https://www.textcontrol.com/blog/2023/09/13/tx-text-control-320-has-been-released/llms.txt)
- [Create a Table of Contents in Windows Forms using C#](https://www.textcontrol.com/blog/2023/01/23/create-toc-in-windows-forms/llms.txt)
- [TX Text Control 31.0 and TX Spell .NET 10.0 Have Been Released](https://www.textcontrol.com/blog/2022/09/07/tx-text-control-31-released/llms.txt)
- [TX Text Control 30.0 and TX Spell .NET 9.0 Have Been Released](https://www.textcontrol.com/blog/2021/11/29/tx-text-control-30-released/llms.txt)
- [Two Ways to Restart Numbered Lists in TX Text Control](https://www.textcontrol.com/blog/2021/11/03/two-ways-to-restart-numbered-lists/llms.txt)
- [Zoom Tricks: Disabling CTRL + MOUSE WHEEL and More](https://www.textcontrol.com/blog/2020/12/09/zoom-tricks-disabling-ctrl-mouse-wheel-and-more/llms.txt)
- [TX Text Control X19 and TX Spell 8.0 Have Been Released](https://www.textcontrol.com/blog/2020/12/02/tx-text-control-x19-released/llms.txt)
- [TX Text Control X18 has been Released](https://www.textcontrol.com/blog/2020/03/16/tx-text-control-x18-released/llms.txt)
- [Merge Excel Documents into MailMerge Templates using IncludeText Fields](https://www.textcontrol.com/blog/2019/08/13/merge-excel-documents-into-mailmerge-templates/llms.txt)
- [MailMerge: Conditional Table Cell Colors using Filter Instructions](https://www.textcontrol.com/blog/2019/06/06/mailmerge-conditional-table-cell-colors-using-filter-instructions/llms.txt)
- [MailMerge: Using Filters to Remove Unwanted Rows](https://www.textcontrol.com/blog/2019/06/04/mailmerge-using-filters-to-remove-unwanted-rows/llms.txt)
- [TX Text Control X17 has been Released](https://www.textcontrol.com/blog/2019/05/28/tx-text-control-x17-released/llms.txt)
- [Create and Modify Adobe PDF Documents within your .NET Application](https://www.textcontrol.com/blog/2019/05/27/create-and-modify-adobe-pdf-documents/llms.txt)
- [Creating Templates: Typical Invoice Elements](https://www.textcontrol.com/blog/2019/04/16/typical-invoice-elements/llms.txt)
- [Text Control Roadmap 2019: High DPI Support, Forms, Node.js and Angular](https://www.textcontrol.com/blog/2019/03/12/text-control-roadmap-2019/llms.txt)
- [ReportingCloud: Open Source Documentation Released](https://www.textcontrol.com/blog/2019/02/11/open-source-documentation-released/llms.txt)
- [MailMerge: Conditional Rendering of Merge Blocks](https://www.textcontrol.com/blog/2018/11/28/mailmerge-conditional-rendering-of-merge-blocks/llms.txt)
- [DataSourceManager: Using the Ready-to-Use Reporting Dialog Boxes](https://www.textcontrol.com/blog/2018/11/27/datasourcemanager-using-the-ready-to-use-reporting-dialog-boxes/llms.txt)
- [TX Text Control X16 and TX Barcode .NET 5.0 Released](https://www.textcontrol.com/blog/2018/11/14/tx-text-control-x16-and-tx-barcode-net-50-released/llms.txt)
