# Mail Merge: Suppress Lines with Empty Merge Fields

> Address blocks in mail merge output can contain blank lines when merge fields have no data. Setting RemoveEmptyFields to false and then iterating through ApplicationFields after merging allows programmatic detection and removal of lines where all fields are empty in the template.

- **Author:** Bjoern Meyer
- **Published:** 2014-08-01
- **Modified:** 2026-07-17
- **Description:** Address blocks in mail merge output can contain blank lines when merge fields have no data. Setting RemoveEmptyFields to false and then iterating through ApplicationFields after merging allows programmatic detection and removal of lines where all fields are empty in the template.
- **2 min read** (392 words)
- **Tags:**
  - GitHub
  - Reporting
  - Sample
- **Web URL:** https://www.textcontrol.com/blog/2014/08/01/mail-merge-suppress-lines-with-empty-merge-fields/
- **LLMs URL:** https://www.textcontrol.com/blog/2014/08/01/mail-merge-suppress-lines-with-empty-merge-fields/llms.txt
- **LLMs-Full URL:** https://www.textcontrol.com/blog/2014/08/01/mail-merge-suppress-lines-with-empty-merge-fields/llms-full.txt
- **GitHub Repository:** https://github.com/TextControl/TextControl.MailMerge.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 should be removed. The address block is included in a TextFrame, so that following elements are not moved when an empty line is removed.

![Mail Merge: Suppress lines with empty merge fields](https://s1-www.textcontrol.com/assets/dist/blog/2014/08/01/a/assets/template.webp "Mail Merge: Suppress lines with empty merge fields")Thanks to the flexible API of TX Text Control Reporting, it is very easy to realize such requirements.

[MailMerge](https://docs.textcontrol.com/textcontrol/windows-forms/ref.txtextcontrol.documentserver.mailmerge.class.htm) has the property [RemoveEmptyFields](https://docs.textcontrol.com/textcontrol/windows-forms/ref.txtextcontrol.documentserver.mailmerge.removeemptyfields.property.htm) that specifies whether empty fields should be removed from the template or not. By default, this property is true, but this removes only the text and not the complete line.

In order to handle the removal programmatically, this property should be set to *false* to find these fields in the template after it has been merged.

The following code loops through all empty fields and checks whether the field is at the end of a line and the only empty field in that line. If that case is true, the field and the carriage return is removed.

```
private bool RemoveEmptyFieldLines()
{
    foreach (IFormattedText obj in serverTextControl1.TextParts)
    {
        foreach (ApplicationField field in obj.ApplicationFields)
        {
            // check, if character next to empty field is a carriage return
            if (obj.TextChars[field.Start + field.Length].Char == '\n')
            {
                bool bCompleteLine =
                    (field.Start == obj.Lines.GetItem(field.Start).Start)
                    ? true : false;

                // if yes, remove the field
                obj.Selection.Start = field.Start;
                obj.ApplicationFields.Remove(field, false);

                // if the field is the only text in the line
                // remove the CR
                if (bCompleteLine)
                {
                    obj.Selection.Length = 1;
                    obj.Selection.Text = "";
                }

                // call RemoveEmptyFieldLines recursively
                return true;
            }
            else
                field.Text = "";
        }
    }

    return false;
}
```

This method *RemoveEmptyFieldLines* is simply called after the *MergeObjects* method of *MailMerge*:

```
mailMerge1.MergeObjects(reports);

// remove empty lines
bool bMoreLines = true;

do
{
   bMoreLines = RemoveEmptyFieldLines();
}
while (bMoreLines == true);
```

![Mail Merge: Suppress lines with empty merge fields](https://s1-www.textcontrol.com/assets/dist/blog/2014/08/01/a/assets/results.webp "Mail Merge: Suppress lines with empty merge fields")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

- [Reporting: Merging MS Word Documents with DocVariables](https://www.textcontrol.com/blog/2015/06/10/reporting-merging-ms-word-documents-with-docvariables/llms.txt)
- [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)
- [ReportingCloud: Open Source Documentation Released](https://www.textcontrol.com/blog/2019/02/11/open-source-documentation-released/llms.txt)
- [Updated MVC Sample: Loading Files from the Backstage Menu](https://www.textcontrol.com/blog/2017/04/20/updated-mvc-sample-loading-files-from-the-backstage-menu/llms.txt)
- [ASP.NET MVC: Implementing a Simplistic, Custom Button Bar](https://www.textcontrol.com/blog/2017/03/13/aspnet-mvc-implementing-a-simplistic-custom-button-bar/llms.txt)
- [ASP.NET MVC: Adding Protected Sections to Documents](https://www.textcontrol.com/blog/2017/03/01/aspnet-mvc-adding-protected-sections-to-documents/llms.txt)
- [Creating Conference Badges with Text Control Reporting](https://www.textcontrol.com/blog/2017/02/14/creating-conference-badges-with-text-control-reporting/llms.txt)
- [ASP.NET: Adding Electronic Signatures to Documents](https://www.textcontrol.com/blog/2016/10/12/aspnet-adding-electronic-signatures-to-documents/llms.txt)
- [Creating Dynamic HTML Forms Using ReportingCloud](https://www.textcontrol.com/blog/2016/09/28/creating-dynamic-html-forms-using-reportingcloud/llms.txt)
- [MVC: Adding an Electronic Signature to Documents in Web.TextControl](https://www.textcontrol.com/blog/2016/02/18/mvc-adding-an-electronic-signature-to-documents-in-webtextcontrol/llms.txt)
- [MVC: Loading a Data Source from a View Model](https://www.textcontrol.com/blog/2016/01/29/mvc-loading-a-data-source-from-a-view-model/llms.txt)
- [MVC: Merging Templates in a Controller HttpPost Method](https://www.textcontrol.com/blog/2016/01/19/mvc-merging-templates-in-a-controller-httppost-method/llms.txt)
- [HTML5: Display and Handle FormCheckBox Fields](https://www.textcontrol.com/blog/2015/09/22/html5-display-and-handle-formcheckbox-fields/llms.txt)
- [Create Database Excerpt Files from Assemblies](https://www.textcontrol.com/blog/2015/08/25/create-database-excerpt-files-from-assemblies/llms.txt)
- [Merging Documents with RESTful Web API's](https://www.textcontrol.com/blog/2015/07/31/merging-documents-with-restful-web-apis/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)
- [Reporting: Styling the DocumentViewer for ASP.NET](https://www.textcontrol.com/blog/2015/06/14/reporting-styling-the-documentviewer-for-aspnet/llms.txt)
- [Building a Touch-enabled Button Bar with Javascript](https://www.textcontrol.com/blog/2015/05/27/building-a-touch-enabled-button-bar-with-javascript/llms.txt)
- [MailMerge: Merge Hyperlinks into Merge Fields](https://www.textcontrol.com/blog/2015/05/15/mailmerge-merge-hyperlinks-into-merge-fields/llms.txt)
- [MailMerge: Conditional INCLUDETEXT Fields](https://www.textcontrol.com/blog/2015/01/08/mailmerge-conditional-includetext-fields/llms.txt)
- [Alternate Row Colors Using NEXT Fields](https://www.textcontrol.com/blog/2013/12/30/alternate-row-colors-using-next-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)
- [Creating Reports with TX Text Control](https://www.textcontrol.com/blog/2003/03/28/creating-reports-with-tx-text-control/llms.txt)
