Nested regions with paragraph break : InvalidOperationException : Stack Empty

Hello,

I’m getting an InvalidOperationException (Stack Empty) with the attached template.
I’m using this code to reproduce this issue :

Document doc = new Document(@"C:\partage\PileVide.docx");
DataSet ds = new DataSet();
for (int i = 0; i < 9; i++)
{
   ds.Tables.Add();
   ds.Tables[i].Columns.Add("test");
   ds.Tables[i].Rows.Add("a");
}
doc.MailMerge.ExecuteWithRegions(ds);

Stack trace :

à System.Collections.Stack.Pop()
à xfbd1009a0cbb9842.x109ecce6e2a0da51.x47f176deff0d42e2()
à Aspose.Words.Fields.Field.x58daf9beaf8b1ae0()
à Aspose.Words.Fields.Field.x42a25ae95099edb8(x5e36356bc92c609b x0f7b23d1c393aed9)
à Aspose.Words.Fields.Field.x295cb4a1df7a5add(x5e36356bc92c609b x0f7b23d1c393aed9)
à xfbd1009a0cbb9842.xfedf115fd9c03862.x4e3cfc222c92cda7(Field xe01ae93d9fe5a880, x5e36356bc92c609b x0f7b23d1c393aed9)
à xfbd1009a0cbb9842.xbf9ddf72e1283af9.x18dfca7c5fd2402f()
à xfbd1009a0cbb9842.xfedf115fd9c03862.xdd6cf0348a23f220(xcf417e2db4fe9ed3 xe00c282e1a49fcfb)
à xfbd1009a0cbb9842.xfedf115fd9c03862.xdf269951086089ce(x6435b7bbb0879a04 xa942970cc8a85fd4)
à xe86f37adaccef1c3.xc5c3f438428cb03b.xdeeb682062ef79a5()
à Aspose.Words.Reporting.MailMerge.ExecuteWithRegions(IMailMergeDataSourceRoot dataSourceRoot)
à Aspose.Words.Reporting.MailMerge.ExecuteWithRegions(DataSet dataSet)

As information, I tried to reproduce the issue using this code to create a template. I don’t see any difference with the attached template, but it works fine with this code …

DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertField(" MERGEFIELD TableStart:TABLE1 ");
Field field = builder.InsertField("IF \"-1\" = \"-1\" \"a " + Environment.NewLine);
builder.InsertField(" MERGEFIELD TableEnd:TABLE1 ");
builder.MoveTo(field.Separator);
builder.InsertField(" MERGEFIELD TableStart:TABLE8 ");
builder.InsertNode(new Run(doc, " b "));
builder.InsertField(" MERGEFIELD TableEnd:TABLE8 ");
builder.InsertNode(new Run(doc, " c" "" "));

I reproduce this issue with Aspose.Word 10.5.0.

I tried with Aspose 11.7.0, I’m getting this exception : System.InvalidOperationException : Mail merge regions ‘TABLE1’ and ‘TABLE1’ overlap. Two regions cannot start/end in the same paragraph or occupy the same table row.
Nested Region are no more supported by Aspose Word ?

Hi,

Thanks for your inquiry. Please note that nested MailMerge regions are supported in Aspose.Words. I would suggest you please read the following article on How to use Nested Mail Merge regions:
https://docs.aspose.com/words/net/nested-mail-merge-with-regions/

Secondly, I investigated the structure of the IF field in your document and found that this exception occurs because your IF field is not well-formed. Please try copying the IF field from the attached Word document in your ‘PileVide.docx’ document. I hope, this will fix the problem.

Best Regards,

Hi,

The document was generated by one of our customer, using our Word plugin (Fields are inserted using VSTO). The produced document is merge with data using Aspose.Words.
I can’t modify this document, but I’ve to fix this bug in our plugin.
Could you please explain me why this IF field is not well-formed ? It works fine in other case.

Thanks

Hi,

Thanks for your inquiry. Well, for this particular case, you may use the following code as a work around:

Document doc = new Document(@"C:\test\PileVide.docx");
doc.Range.Bookmarks.Clear();
doc.JoinRunsWithSameFormatting();
foreach (Node n in doc.FirstSection.Body.Paragraphs[1].ChildNodes)
doc.FirstSection.Body.FirstParagraph.AppendChild(n.Clone(false));
doc.FirstSection.Body.Paragraphs[1].Remove();
DataSet ds = new DataSet();
for (int i = 0; i < 9; i++)
{
    ds.Tables.Add();
    ds.Tables[i].Columns.Add("test");
    ds.Tables[i].Rows.Add("a");
}
doc.MailMerge.ExecuteWithRegions(ds);
doc.Save(@"C:\test\out.docx");

Best Regards,