Free Support Forum - aspose.com

Avoid having a blank line if a merge field is null

Hi,

Using Aspose.Word Mail Merge, I am trying to fill dynamic data from a database table to a Word template with the following merge fields:

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

«CustomerLastName», «CustomerFirstName» «CustomerMI»

«CustomerAddress1»

«CustomerAddress2»

«CustomerCity» «CustomerState» «CustomerPostalCode»

«CustomerPhone»

How can I avoid a blank line if CustomerAddress2 field is null?

Please advise.

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. I think that you should use RemoveEmptyParagraphs property. See the following link for more information.

http://www.aspose.com/documentation/file-format-components/aspose.words-for-.net-and-java/aspose.words.reporting.mailmerge.removeemptyparagraphs.html

Also see the following code example and attached documents.

//Prepare datasource

DataTable table = new DataTable();

table.Columns.Add("CustomerLastName");

table.Columns.Add("CustomerFirstName");

table.Columns.Add("CustomerMI");

table.Columns.Add("CustomerAddress1");

table.Columns.Add("CustomerAddress2");

table.Columns.Add("CustomerCity");

table.Columns.Add("CustomerState");

table.Columns.Add("CustomerPostalCode");

table.Columns.Add("CustomerPhone");

//Add data

table.Rows.Add(new object[] { "Noskov", "Alexey", "Mr", "My address", null, "<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />Auckland", "NZ", "61202", "98479349863"});

//Open template

Document doc = new Document(@"Test050\in.doc");

//Specifies whether paragraphs that contained mail merge

//fields with no data should be removed from the document.

doc.MailMerge.RemoveEmptyParagraphs = true;

//Execute mail merge

doc.MailMerge.Execute(table);

//Save document

doc.Save(@"Test050\out.doc");

Hope this helps.

Best regards.