Conditional merge field is not handled by documentbuilder.insertHtml()

Hello Aspose Team

The following issue is reported in Aspose.Words 13.11.0.0

We have implemented IFieldMergingCallback.FieldMerging() to merge html data into the document. This is working great.
But when there is a conditional merge field statement in the document it does not work and error is reported in the merge document.

Error! Unknown op code for conditional.

For Non-Html processed fields the conditional states works.
Please suggest what do we need to do differently to have conditional statement work for the Html processed fields.

Thank you.

Hi there,

Thanks for your inquiry. It would be great if you please share following detail for investigation purposes.

  • Please attach your input Word and Html document.
  • Please create a standalone/runnable simple application (for example a Console Application Project) that demonstrates the code (Aspose.Words code) you used to generate your output document
  • Please attach the output Word file that shows the undesired behavior.

As soon as you get these pieces of information to us we’ll start our investigation into your issue.

Thank you for reply. I have attached a demo with document to merge and the merged document too. You will notice that the conditional IF statement does not work properly for the field which is merged as a Html field.

Thanks again for looking into the issue.

Hi there,

Thanks for sharing the detail. We have tested the scenario and have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-12816. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

Thank you Tahir for Reporting the issue. Our clients are having this issues with merging the documents and we will really appreciate a priority take on it. We appreciate your support.

Hi there,

Thanks for your inquiry. It is to inform you that our product team has completed the work on this issue (WORDSNET-12816) and has come to a conclusion that this issue and the undesired behavior you’re observing is actually not a bug in Aspose.Words. So, we have closed this issue as ‘Not a Bug’. We are quoting developer’s comments here for your reference.

There is the following field in template:

{IF {MERGEFIELD RepName } ="" "RepName TRUE case." "RepName FALSE Case."}

While mailmerge { MERGEFIELD RepName } is replaced to “” (see FieldMergingCallback) and text “AndrewGel” is inserted before mergefiled.

Field IF after mailmerge:
{IF AndrewGel"" ="" "CaseProdName TRUE case." "CaseProdName FALSE Case."}

To avoid this behavior (both result of mergefield “AndrewGel” and “”) you should use DocumentBuilder.MoveToMergeField instead of DocumentBuilder.MoveTo method. Please check following highlighted code snippet.

public void FieldMerging(FieldMergingArgs args)
{
    DocumentBuilder builder = new DocumentBuilder(args.Document);
    switch (args.DocumentFieldName.ToUpper().Trim())
    {
        case "CASEPRODNAME":
            builder.MoveToMergeField(args.FieldName);
            builder.InsertHtml((string)args.FieldValue);
            args.Field.Remove();
            // The HTML text itself should not be inserted.
            // We have already inserted it as an HTML.
            args.Text = "";
            break;
        default:
            break;
    }
}

Thank you for reply.
I changed the code, and it worked when CaseProdName is AndrewGel.

I changed the conditional statement to following:

{IF «CaseProdName» ="AndrewGel®" "CaseProdName TRUE case." "CaseProdName FALSE Case."}

CaseProdName value as “AndrewGel®” but the merge result is CaseProdName FALSE Case.

Can you please check and let me know how to resolve this issue?

Thank you so much.

Hi there,

Thanks for your inquiry. We have tested the scenario using latest version of Aspose.Words for .NET 15.12.0 and have not found the shared issue. Please use Aspose.Words for .NET 15.12.0. We have attached the modified input and output documents with this post for your kind reference.

Hi Tahir,

We have tested in the latest version and planning to upgrade very soon.
As this issue is the production issue for one of our client . They are not ready to take full upgrade at this moment.
I would really appreciate if you can provide a work around fix for this.

Thanks for your help

Hi there,

Thanks for your inquiry. We don’t provide support for older releases of Aspose.Words. All fixes and new features are always added into new versions of our products. So, you will have to upgrade if you need new features or fixes.

Please try the solution with older version of Aspose.Words shared in this post. Hope this helps you.