We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Mail Merge not formatted correctly when TableStart inside an IF multiple times

I have a use-case where I want to render content on a third level conditionally based on a value in the second level.

The section of the Mail Merge template that is relevant looks something like this:

{ MERGEFIELD TableStart:Child}

{ IF { MERGEFIELD ChildCondition } = "Yes" "{ MERGEFIELD TableStart:Grandchild }{ MERGEFIELD Field1}{ MERGEFIELD TableEnd:Grandchild }"

"{ MERGEFIELD TableStart:Grandchild }{ MERGEFIELD Field2}{ MERGEFIELD TableEnd:Grandchild }" }

{ MERGEFIELD TableEnd:Child}

Notice how I want to render a list of Field1 items from the Grandchild level when the condition is true and a list of Field2 items from the Grandchild level when the condition is false.

It's hard to describe what the actual document looks like except to say it is not correct. I have attached a sample application to replicate the issue. I have included the actual output and the expected output.

Hi there,


Thanks for your inquiry. I have worked with your document and code have noticed that you are using mail merge with regions inside IF field. In your case I suggest you please do not use nested regions. Please modify your template document as shown below to get the required output. I have attached the modified template document with this post for your kind reference. Hope this helps you.

{ MERGEFIELD TableStart:Child }
{ IF { MERGEFIELD ChildCondition } = “Yes” “{ MERGEFIELD Field1 }” “{ MERGEFIELD Field2 }” }
{ MERGEFIELD TableEnd:Child }

var doc = new Document(MyDir + “MergeTemplate.docx”);<o:p></o:p>

var dt = new DataTable("Child");

dt.Columns.Add("ChildCondition");

dt.Columns.Add("Field1");

dt.Columns.Add("Field2");

dt.Rows.Add("Yes", "Field1", "Field2");

dt.Rows.Add("No", "Field1", "Field2");

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;

doc.MailMerge.ExecuteWithRegions(dt);

doc.Save(MyDir + "Out.docx");

It is impossible for us to eliminate nested regions in our production code.

Sure, I can change the simple example to not use nested regions. But my simple example is just to provide you an example of the issue we are having in our production code. Our production code is much more complex and nested regions is something we must be able support.

Hi there,


Thanks for your inquiry. You are using mail merge with regions inside IF field. In this case, everything will be repeated for nested region including IF field. So, the behavior of Aspose.Words is correct.

In your case, I suggest you please modify your template document. Insert a table with one row and one cell in IF field result. Please check the attached modified template and output document.

Moreover, I suggest you please read following documentation link to replace Fields with Static Text. I have attached the modified MailMergeDocument.cs with this post. Hope this helps you.
http://www.aspose.com/docs/display/wordsnet/How+to++Replace+Fields+with+Static+Text


ParentDataSource parentDataSource = new ParentDataSource();<o:p></o:p>

ChildDataSource childDataSource = new ChildDataSource();

Builder.Document.MailMerge.Execute(parentDataSource);

Builder.Document.MailMerge.ExecuteWithRegions(childDataSource);

FieldsHelper.ConvertFieldsToStaticText(Builder.Document, FieldType.FieldIf);