Products Technologies Demo Docs Blog Support Company

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.

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 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

Thanks to the flexible API of TX Text Control Reporting, it is very easy to realize such requirements.

MailMerge has the property RemoveEmptyFields 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

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

ReportingWindows FormsGitHub

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…


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.


ReportingDocumentationGitHub

ReportingCloud: Open Source Documentation Released

The ReportingCloud documentation hub provides detailed endpoint references organized by group, getting started articles, and code samples for .NET, PHP, and Java. The open-source docs are hosted…


ReportingGitHubHTML5

Updated MVC Sample: Loading Files from the Backstage Menu

The ASP.NET MVC backstage menu sample has been updated to TX Text Control X14 with the latest NuGet packages. The backstage view replaces the default file menu with a customizable, MS Word-style…


ASP.NETReportingGitHub

ASP.NET MVC: Implementing a Simplistic, Custom Button Bar

This ASP.NET MVC sample replaces the Web.TextControl ribbon bar with a custom button bar built in HTML, CSS, and JavaScript. Toggle buttons apply formatting commands like bold, while the…

Share on this blog post on: