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

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.

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

Download the sample from GitHub and test it on your own.
Download and Fork This Sample on GitHub
We proudly host our sample code on github.com/TextControl.
Please fork and contribute.
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.
Related Posts
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…
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.
ReportingCloud: Open Source Documentation Released
We just released a new documentation hub for our Web API ReportingCloud. This documentation is hosted on GitHub and users can contribute.
Updated MVC Sample: Loading Files from the Backstage Menu
We just updated the very popular sample Loading files from the backstage menu to TX Text Control version X14 (24.0) on GitHub. This sample shows how to replace the file menu with an MS Word-style…
ASP.NET MVC: Implementing a Simplistic, Custom Button Bar
For some applications, the fully-featured ribbon bar might be too overloaded with features or the ribbon concept is not required in a project. Programmatically, all ribbon tabs, groups and buttons…