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

Free Support Forum - aspose.com

Conditional merge field will not handle empty string values

Hi,

Previously, we have been able to perform mail merge using Aspose.Words 10.5.0.0 and have experienced no problems passing in a mail merge field with the value “”. When running Aspose.Words 13.05, we get the following text in the document:

Error! Unknown opcode for conditional.

When a non-empty value is provided in the merge field, it behaves as we would expect and displays the correct text defined in the conditional merge field.

Please let me know if you need any further information. I have included a screenshots showing the original document and the observed results.

Hi Matthew,


Thanks for your inquiry. Could you please attach all three of these Word documents along with code here for testing? I will investigate the issue on my side and provide you more information.

Best regards,

Hi,

I have attached the three Word documents you have requested.

I should have some sample code available in the next few hours and will upload that when it is ready.

Regards,

Matthew

Hi,

I have attached the code you requested along with a few sample templates that currently fail and should work when the issue is fixed.

To get the code working make sure the licence and TemplateDirectory variable have been correctly configured. The ‘TemplateDirectory’ should contain the template files provided and should have read / write permissions.

From what I can gather, when instructing the document builder to write the value of a merge field, it correctly handles non-conditional merge fields. However, when dealing with conditional merge fields there are no double quotes being applied to the text outputted - causing problems for empty string values and values containing spaces.

The code below shows how mail merge fields are applied in our case.

foreach (var task in tasks)
{
var doc = new Aspose.Words.Document(task.Template);

doc.MailMerge.FieldMergingCallback = new FieldMerger();
doc.MailMerge.Execute(new[] { “diagnosis” }, new object[] { task.MailMergeText });
doc.MailMerge.FieldMergingCallback = null;
doc.Save(task.Output, Aspose.Words.SaveFormat.Docx);

}


Below is the definition of FieldMerger.

using Aspose.Words;
using Aspose.Words.Reporting;
 
namespace AsposeSandbox
{
public class FieldMerger : IFieldMergingCallback
    {
public void FieldMerging(FieldMergingArgs args)
        {
var builder = new DocumentBuilder(args.Document);
            args.Text = "";
            if (!builder.MoveToMergeField(args.FieldName, true, true))
{
                // merge field does not exist
return;
            }
            builder.Write((string) args.FieldValue);
}
 
public void ImageFieldMerging(ImageFieldMergingArgs args)
        {
// no op
        }
}
}

Hoping this is all the information needed to diagnose this issue.

Regards,
Matthew

Hi Matthew,


Thanks for the additional information. I tested the scenario and have managed to reproduce the same problem on my side. For the sake of correction, I have logged this problem in our issue tracking system as WORDSNET-8461. We will further look into the details of this problem and keep you updated on the status of correction. We apologize for your inconvenience.

Best regards,

Hi,

Just wanting to confirm that the issue described in the forum thread does appear to be an regression from version 10.5 of Aspose Words.

I have run the same code and templates using Aspose Words 10.5 and can confirm that the merge fields contained with conditional fields were behaving as we expected.

Hope this information is useful.

Regards,

Matthew

Hi Matthew,

Thanks for your inquiry. Our development team has completed the work on your issue (WORDSNET-8461) and has come to a conclusion that this issue and the undesired behaviour you're observing in Aspose.Words 13.5.0 is actually not a bug. So, we will most likely close this issue as 'Not a Bug'.

The problem occurs because you are replacing a non-quoted nested MERGEFIELD in an outer IF field's code with an empty string which generates IF's code like { IF = "" "Empty" "Something" }, i.e. it does not contain the first argument of the comparison. Aspose.Words 13.5.0 does not process such a field any longer but Aspose.Words 10.5.0 did which is because of an undocumented bug.

However, if you try to update the IF field output document using Microsoft Word editor, you will see that it fails updating it either. Therefore, Aspose.Words behaves more like Microsoft Word now.

Moreover, to resolve the issue please replace the following code line:
builder.Write((string)args.FieldValue);

with the following one:
builder.Write(string.Format("\"{0}\"", args.FieldValue));

I hope, this helps.

Best regards,