Replace Nested Merge Fields in Conditional IF Statements True & False Parts during Word Document Mail Merge using C# .NET

Please help. I’m trying to diagnose a complex problem with mail merge fields inside conditional IF statements in Word templates created by our customers.

As a first step, I’m trying to create a POC that demonstrates the problem, however in my POC I can not get the IF statements to evaluate AT ALL. The basic mail merge works (most of the time) but the IF statements do NOTHING and come through unchanged

I’m using Aspose.Words v19.10 for .NET, building sample project against .NET framework 4.5.2.

What is wrong with this sample code?

 // Load template
 string dataDir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
 
 string inPath = Path.Combine(dataDir, "MergeMe.docx");
 Document doc = new Document(inPath);
 
 //doc.MailMerge.UseNonMergeFields = true; // INTERESTNG - See Aspose help
 
 // Fill the fields in the document with user data.
 doc.MailMerge.Execute(
     new string[] { "Key1", "Key2", "Key3"},
     new object[] { "Value1", "Value2", "Value3"});
 
 string outPath = Path.Combine(dataDir, Path.GetFileNameWithoutExtension(inPath) + "_" + DateTime.Now.ToString("HHmmss") + ".docx");
 doc.Save(outPath);

See attached output:

Template and Console App (without the license file) attached.

AsposePOC.zip (4.9 MB)

@pcleaveland,

We are working on your query and will get back to you soon.

@pcleaveland,

It seems, Aspose.Words’ mail merge engine works in the following way:

  1. When IF field’s expression evaluates into True AND merge field is inside the ‘True Part’ of IF field, then merge field will be replaced by its Value.

  2. When IF field’s expression evaluates into False AND merge field is inside the ‘False Part’ of IF field, then merge field will be replaced by its Value.

However, we tested the scenario and have managed to reproduce the same problem on our end i.e. some merge fields nested inside IF fields are not getting replaced by their values. For the sake of any correction, we have logged this problem in our issue tracking system. The ID of this issue is WORDSNET-20008. We will further look into the details of this problem and will keep you updated on the status of linked issue. We apologize for your inconvenience.

@pcleaveland,

Regarding WORDSNET-20008, we have completed the work on your issue and concluded to close this issue with “Not a Bug” status. Please see the following analysis details:

All IFs are evaluated properly. See attached document (Test20008 Out.zip (10.9 KB)). The document on your screenshot is in the field codes mode, and if we see at the field results in the real document, all of them are correct. Our assumption is that you expect the IFs at the top to be removed from the document and replaced with results (just like MERGEFIELDs), but it does not happen during mail merge. If you need to achieve that, then you need to call doc.Range.UnlinkFields() after the mail merge. Or else, please tell us why do you think that the IF fields are not updating.

Secondly, MERGEFIELDs inside IF fields are not updating (staying in the document) because the false expression (i.e. which is considered false after IF’s evaluation) is not evaluated.