Mail Merge template with conditional logic break after upgrade to Aspose.Words.16.8.0

#1 (71.4 KB)

This issue started coming after upgrade of Aspose.Words FROM 14.2.0 TO 16.8.0.

If you don’t have Conditional Logic in the Mail Merge, it comes out clean once exported. If you have Conditional Logic in the Mail Merge, it has issues and mail merge code is returned instead of values in most fields. This was working fine before Aspose.words upgrade.

If a template has a conditional statement (an IF statement and/or a table start/table end AND the fields referenced are from multiple applications that are crossed reference to each other, in on of the fields, the mail merge code for table start or the IF statement are returned instead of the values of the respective fields.

Attached “Mail_Merge_template_code_w_conditions.docx” template which is with if condition returns mail merge code instead of value. see the result attached “Mail_Merge_example_output_w_conditions.docx”
Attached “Mail_Merge_template_code_wo_conditions.docx” template which is without if condition gives values which is CORRECT. see the result attached



Thanks for your inquiry. Please create a standalone console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing. We will investigate the issue on our side and provide you more information.

PS: Please zip and upload the application.


Attached are the files which has mail merge logic. Please check and let us know if this need any tweaking of the code to (10.1 KB)
the Aspose upgrade changes.



Thanks for sharing the detail. Please set the value of MailMerge.MergeDuplicateRegions property as true before executing mail merge with regions to get the desired output. This property gets or sets a value indicating whether all of the document mail merge regions with the name of a data source should be merged while executing of a mail merge with regions against the data source or just the first one.

doc.MailMerge.MergeDuplicateRegions = true;


Thanks for your prompt reply. As per your suggestion I have added Document.MailMerge.MergeDuplicateRegions = true; in my file MailMergeDocument.cs at below place. But it does not seems to work.

Document.MailMerge.MergeDuplicateRegions = true;

Also in my new template(Which is attached - MAIL_MERGE_TEMPLATE_COMPANY_WITH_IF.docx and MAIL_MERGE_TEMPLATE_COMPANY_WITHOUT_IF) I have kept One Table inside If statement (Without Duplicate), and the mail merge Output is showing blank value in Division Table value(attached doc : Mail_Merge_Document_With_IF_Output.doc). Where as Table without IF condition has Values in Division Table (attached doc: Mail_Merge_Document_Without_IF_Output.doc) (25.0 KB)



Thanks for your inquiry. We have tested the scenario using latest version of Aspose.Words for .NET 17.8 with following code example and have not found the shared issue. Please upgrade to the Aspose.Words for .NET 17.8. We have attached the three output documents with this post for your kind reference.
output (24.7 KB)

Document doc = new Document(MyDir + "Mail_Merge_template_code_w_conditions.docx");

DataTable table = new DataTable();
table.Columns.Add("LIST:xref_test.Value", typeof(string));
table.Columns.Add("Company", typeof(string));
var newRow = table.NewRow();
newRow["LIST:xref_test.Value"] = "Yes";
newRow["Company"] = "Test Company";

DataTable table2 = new DataTable("Divisions");
table2.Columns.Add("Division", typeof(string));
var newRow2 = table2.NewRow();
newRow2["Division"] = "Test Division";

doc.MailMerge.MergeDuplicateRegions = true;

doc.Save(MyDir + "Mail_Merge_template_code_w_conditions output 17.8.docx");


I have upgraded to Aspose.Words 17.8.0 and still facing the same issue. Even the output document(output ) you have generated and attached has the issue. If you look at your output(Mail_Merge_template_code_w_conditions output 17.8.docx) the first Division Name is still coming as blank and the next Division Name under the table is still showing the mail merge code instead of values.

My issue looks similar to this issue but could not see the solution to it -



Thanks for your inquiry. To ensure a timely and accurate response, please create a standalone console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing. As soon as you get this application ready, we’ll start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: please zip and upload your application.


I have already uploaded the file which has the logic for mail merge. Can you please use the code to create the stand alone application as you might already have the test project available as this will take me considerable amount of time. Also my issue is similar to below link where one of your developer have worked upon but the solution is not available. This should be a very normal use case(if statement with dynamic table inside) Let me know if you need any further information and you want me to change few lines of code.



Thanks for your inquiry. Unfortunately, it is difficult to reproduce the issue without simplified code example. We have used your shared .cs files to create console application but we are getting compile time error. The issue shared in this forum thread can be fixed by using MailMerge.MergeDuplicateRegions property. However, we need simplified code example without compilation errors to reproduce this issue at our end. if you cannot supply us the application we will not be able to investigate your issue and raise a ticket. Thanks for your cooperation.


I will try to create standalone application. In the mean time can you please let me know the solution for below thread as my issue is similar to below issue. Also while debugging I found that WITH IF condition, while executing “Document.MailMerge.ExecuteWithRegions(m_currentWrapper);” does not invoke MoveNext() method

public bool MoveNext()
return m_enumerator.MoveNext();

which was overriden in MailMergeDataSource : IMailMergeDataSource class . Where as in working condition WITHOUT IF condition it invokes MoveNext() method and enumerate all the value in the table which is the reason it is not working with If condition.



Thanks for your inquiry. We will write the code example that implements IMailMergeDataSource for your scenario and will share our finding here for your reference as soon as possible.



Thanks for your inquiry. We have created a simplified console application for your scenario. We have tested the scenario and have not found any issue. Please check the attached application and use following mail merge properties in your code. Hope this helps you. (70.9 KB)

doc.MailMerge.MergeDuplicateRegions = true;
doc.MailMerge.MergeWholeDocument = true;


Thanks. It worked for me after setting these two mail merge properties you have mentioned. but with one exception
I had to reset the Mail Merge cleanup option to Not use MailMergeCleanupOptions.RemoveContainingFields. With RemoveContainingFields it does Not work. Below is my modified code. Can you please check if it needs further modifications and why it is not working with RemoveContainingFields

while (m_dataSources.Count != 0)
m_currentWrapper = m_dataSources.Dequeue();
Document.MailMerge.CleanupOptions = MailMergeCleanupOptions.None;
Document.MailMerge.CleanupOptions |= MailMergeCleanupOptions.RemoveEmptyParagraphs;
Document.MailMerge.CleanupOptions |= MailMergeCleanupOptions.RemoveUnusedFields;
Document.MailMerge.CleanupOptions |= MailMergeCleanupOptions.RemoveUnusedRegions;
Document.MailMerge.MergeDuplicateRegions = true;
Document.MailMerge.MergeWholeDocument = true;
if (string.IsNullOrEmpty(m_currentWrapper.TableName))



Thanks for your inquiry. Please use mail merge cleanup options before executing the MailMerge.ExecuteWithRegions method. Hope this helps you.