Hiding detail tables when multiple details in master detail merge

Hello. I am creating a solution similar to the product catalog demo, only I have several detail regions for a given master region. My requirement is to hide any detail table if there is no data returned for that given detail. I am easily able to remove a table using a conditional statement with a known table index such as:

DataTable dt = //go get data
if (dt.Rows.Count == 0) doc.Sections[0].Body.Tables[2].Remove();
else doc.MailMerge.ExecuteWithRegions(dt);

However, the template for this solution will be modified by the end customer, and the order of the tables may change (or a table may be deleted entirely). Is there a way to programmitically determine which table I need to remove so that I don’t have to hardcode the index in there?
Thank you in advance for your help.
ETA: can I access the table by searching for the TableStart: merge field? If so, how would I do that?

Hi
Thanks for your request.

  1. You can insert bookmark into your table and then remove table where this bookmark is placed. See the following code:
//Open template
Document doc = new Document(@"Test036\in.doc");
//Get table by bookmark
Table tab = doc.Range.Bookmarks["mybk"].BookmarkStart.GetAncestor(NodeType.Table) as Table;
//Remove table
if (tab != null)
    tab.Remove();
//Save output document
doc.Save(@"Test036\out.doc");
  1. You can get table by TableStart mergefield. See the following code:
//Open template and create Documentbuilder
Document doc = new Document(@"Test036\in.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
//Move document builder cursor to mergefield
//When you move docBuilder cursor to mergefield like TableStart:myTable
//You should specify only table name without "TableStart:"
builder.MoveToMergeField("myTable");
//Get table
Table tab = builder.CurrentParagraph.GetAncestor(NodeType.Table) as Table;
//Remove table
if (tab != null)
    tab.Remove();
//Save output document
doc.Save(@"Test036\out.doc");

I hope this could help you.
Best regards.

Hi.
Option 2 did not work when implemented with more than one detail region. Option 1 worked.
Thank you much!!

Hi
Thanks for additional information. If you want to test Aspose.Words without the evaluation version limitations, you can request a 30-day Temporary License. Please refer to
https://purchase.aspose.com/temporary-license
Best regards.

Thank you! Awesome product!!