IF field containing mail merge field returns incorrect value after performing mail merge using C#

We are using Aspose.Words 19.10 in .NET Framework v4.5.2.

When merging templates created by our customers, we are seeing logical errors with IF tag evaluation during MailMerge. I compared the outcome vs the same merge for the same template done by Word.exe. Aspose is giving the exact opposite result, when MERGERFIELD tags are included.

Template and comparison attached. As you can see from the example below:

  • Basic MERGEFIELD tags work correctly
  • Basic IF tags work correctly, when the values involved are simple constants
  • But, when I use a MERGEFIELD as the subject of the IF tag, Aspose returns the exact OPPOSITE result

What’s happening and how can we fix it?

I tried to look at this behavior using the IFieldMergingCallback Callback. I expected to get callbacks for field types FieldIf = 7 (and/or FieldNextIf = 42 and/or FieldSkipIf = 43), but I got NO CALLBACKS durring the IF evaluations.

I tested MailMerge.UnconditionalMergeFieldsAndRegions, but it made no difference.

The solution can NOT involve humans modifying the templates. These are created by our customers, and MUST give the SAME output in Aspose as they do in when merged by Word.exe. Subtle Font differences are OK, we can live with that, but logical errors can NOT be tolerated.

Attached:

@pcleaveland

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 and provide you more information about your query.

@tahir.manzoor,

I knew you would say that. I tried. Aspose has no working example that executes IF conditions. And for some reason, IF statements do not execute AT ALL in my standalone project. See related thread Replace Nested Merge Fields in Conditional IF Statements True & False Parts during Word Document Mail Merge using C# .NET

I will try again to get my console app working.

Meanwhile, please TRY to help with this issue.

We have customers depending on this, and I it hurts us to wait 24 hours for each response.

@pcleaveland

Unfortunately, it is difficult to say what the problem is without working code example. Please share the simple code example for testing.

Your template document contains mail merge fields. You may also share the values of mail merge fields that you are using at your end. We will write the code and test this case. Thanks for your cooperation.

I’ve dug into this more, and it looks like there is a strange Word behavior going on when the Templates (and the IF tags) are being edited by the customer.

If they edit the IF tags inline, the changes do not necessarily work as expected. But, if they right click and do “Edit Tag” using the dialog box in Word, then they behave differently. It’s very odd. It seems like some kind of refresh behavior is needed on the IF tags after the {MERGEFIELD} substitutions have happened.

I will attach more details with screen shots when I have fully characterized it.

Then I will need to look for a way to create teat behavior grammatically with Aspose.

It looks like the latest version of Aspose.Words (20.2) handles our templates correctly.

The version in production is 19.1, which is malfunctioning.

Will test more use cases to prove upgrade solves all the anomalies.

@pcleaveland

It is nice to hear from you that your problem has been solved using latest version of Aspose.Words. Please share your test case if face any problem with Aspose.Words.