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,
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,
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