TX Text Control Blog

Mail Merge: Suppress lines with empty merge fields

Björn Meyer

Blogged by Björn Meyer on August 1, 2014 and tagged with sample, reporting, github.

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 void 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 == '\r') { 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 RemoveEmptyFieldLines(); } else field.Text = ""; } } }

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

mailMerge1.MergeObjects(reports); // remove empty lines RemoveEmptyFieldLines();
Mail Merge: Suppress lines with empty merge fields

Give it a try and download the sample project directly from our GitHub repository:

Download and fork this sample on GitHub

We proudly host our sample code on github.com/textcontrol. Feel free to fork and contribute!

Download ZIP

Open repository on GitHub

Open in Visual Studio

Requirements for this sample

  • at least Visual Studio 2012
  • At least a trial version of TX Text Control .NET for Windows Forms ASP.NET X12 (22.0)
Call us: +1 704-544-7445 (United States)





Buy Now

About Text Control

Founded in 1991, Text Control is an award-winning Visual Studio Industry Partner and leading vendor of word processing and reporting components for Microsoft development technologies. The products help thousands of developers add comprehensive word processing functionality to their applications. Text Control is 100% self-owned and independent of all investor decisions. Customers benefit from these years of experience, large user-base, and at the same time, appreciate developing with a mature, reliable product.

SD Times 100SD Times 100SD Times 100SD Times 100SD Times 100SD Times 100Visual Studio 2015 Launch PartnerVisual Studio 2013 Launch PartnerVisual Studio 2012 Launch PartnerVisual Studio Partner