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

Free Support Forum - aspose.com

Tracing merging fields during MailMerge execution

Hi,
I’m trying to trace which tables and fields are merged in the document (the template can be in fact customized by the user):

IFieldMergingCallback cbDeco = new TracingFieldMergingCallbackDecorator(etc);
pDocument.getMailMerge().setFieldMergingCallback(cbDeco);
pDocument.getMailMerge().executeWithRegions(pDataSet);

And it usually works.
But if in the document I have a:
{MergeField TableStart:MyTable}
{ SKIPIF { MERGEFIELD CATEGORY } <> “mycategory” }
… some other fields
{MergeField TableEnd:MyTable}

Then the callback is called anyway even if the record is to be skipped and I can’t get any information if the current record is inside a SKIPIF section.
Is there any way to get this information, or to avoid the callback being called when it is not rendered ?

Thank you

@onof80 SKIPIF field is evaluated after the value of mergefield is evaluated otherwise it would be impossible to evaluate the condition. That is why field merging event is fired. You can check whether mergefield is inside SKIPIF field in the IFieldMergingCallback implementation. For example you can use code like the following:

public void FieldMerging(FieldMergingArgs args)
{
    FieldStart fieldStart = args.Field.Start;

    Node currentNode = fieldStart.PreviousSibling;
    while (currentNode!=null && currentNode.NodeType != NodeType.FieldStart)
        currentNode = currentNode.PreviousSibling;

    bool isInsideSkipIfField = (currentNode != null) && (((FieldStart)currentNode).FieldType == FieldType.FieldSkipIf);

    // Do further processing.

    return;
}
1 Like

Thanks @alexey.noskov,
and just another question, the Result prop in the FieldSkipIf evaluate to something whether the condition is met or not ?

Best

@onof80 No, SKIPIF field does not have result. Field result means the displayed text, but this field does not have any value to display it simply instructs mail merge engine how to process the value. However, you can get its field code and calculate the condition yourself.

1 Like