Removing blank lines in tables when not populated with data

Hello,

I was hoping that you may be able to help me out here as I cannot seem to get this to work.

I have a template with a table (DiabetesMgtTable) that contains several lines of optional text. The issue I have is that I do not want the blank lines to appear when there is no data. I can get this to work with a single line (such as the WeightMgtTable) but not when there are multiple lines of optional text.

I have attached the sample template, xml dataset and examples of my problem and what I want the output to look like.

Are you able to provide me with some sample code in vb that will resolve my issue?

Thanks in advance

Hi Joe,

Thanks for your inquiry. I have worked with your shared documents and have found that the DataSet contains empty values for mail merge fields. So the empty rows are created in output document. Please see the attached image for detail.

You can remove such empty rows from tables by using following code snippet. Hope this helps you. Please let us know if you have any more queries.

Document doc = new Document(MyDir + "Sample+Template.doc");

doc.MailMerge.CleanupOptions |= Aspose.Words.Reporting.MailMergeCleanupOptions.RemoveEmptyParagraphs;
doc.MailMerge.CleanupOptions |= Aspose.Words.Reporting.MailMergeCleanupOptions.RemoveUnusedRegions;
doc.MailMerge.CleanupOptions |= Aspose.Words.Reporting.MailMergeCleanupOptions.RemoveUnusedFields;
doc.MailMerge.CleanupOptions |= Aspose.Words.Reporting.MailMergeCleanupOptions.RemoveContainingFields;
DataSet ds = new DataSet();
ds.ReadXml(MyDir + "SampleDataset.xml");
doc.MailMerge.ExecuteWithRegions(ds);
doc.MailMerge.Execute(ds.Tables[0]);
// Remove empty rows
Node[] rows = doc.GetChildNodes(NodeType.Row, true).ToArray();
foreach(Row row in rows)
{
    bool removeRow = true;
    foreach(Cell cell in row.Cells)
    {
        if (cell.FirstParagraph != null)
            removeRow = !cell.FirstParagraph.HasChildNodes;
    }
    if (removeRow)
        row.Remove();
}
doc.Save(MyDir + "out.docx");