Query on Aspose.Words attempting to merge code already contained within the field result of an IF statement

Aspose version: Aspose Words.NET version 23.6.0

Hi,

The attached example merge template contains a simple IF statement which is always true (for example demonstration purposes) which will perform a region merge on table ‘A’. The straightforward sample code below can be used to attempt to merge that region.

public static class IfWithFieldResultExample
{
    private static string _path = @"C:\DELME\AsposeTestCase1\";

    public static void RunMe()
    {
        // Document paths to use
        string docIn = Path.Combine(_path, "in.docx");
        string docOut = Path.Combine(_path, "out.docx");

        // Build the data
        DataTable A = new DataTable("A");
        A.Columns.Add("C1");
        A.Rows.Add(new object[] { "A-C1-R1" });
        A.Rows.Add(new object[] { "A-C1-R2" });
        DataSet ds = new DataSet();
        ds.Tables.Add(A);

        // Do the merge
        Document doc = new Document(docIn);
        //test code
        //foreach (var field in doc.Range.Fields.Where(f => f.Type == Aspose.Words.Fields.FieldType.FieldIf)) field.Result = string.Empty;
        doc.MailMerge.ExecuteWithRegions(ds);
        doc.Save(docOut);
    }
}

The result of the merge is an exception “System.InvalidOperationException: Mail merge regions ‘A’ and ‘A’ overlap. Two regions cannot start/end in the same paragraph or occupy the same table row.

At first glance, the template and code should not generate that exception. However, as I have discovered, the issue is the IF can already contain the calculated field result.

You can see the field result by placing the cursor in the IF of the IF statement and use Shift-F9 to toggle (F9 to toggle back again). So, the message makes sense - as I know you can’t have two regions in the same paragraph.

The IF statement has existing field result as, for the purposes of this demo, I have manually performed an ‘Update Field’ on the IF statement in Word. In the ‘real-world’, existing template documents could have field result in their IF statements – hence the need to make this query.

To get round this, the commented ‘test code’ line in the sample code, finds the IF statements in the document and removes any existing field result before the merge.

The question: is it correct that Aspose is trying to merge the ‘hidden’ merge code in the field result / am I missing something here please? I ask because it feels like over-coding to have to remove the field result from the IF statements in case they happen to have content already.

Additional Info: for our implementation, we must perform the basic mail merge (doc.MailMerge.Execute) after the region merge as we wish to use the mail merge option ‘remove containing fields’. Performing a basic merge with remove containing fields before the region merge actually removes the region in the IF – so that order cannot be used.

Regards,

James

in.docx (14.1 KB)

@JamesMurdoch You are right, the behavior looks like a bug.

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): WORDSNET-27051

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

Thanks for the quick reply Alexey – appreciated.

So, unless you know of a specific reason not to in Aspose, I’ll assume a work around for this is to cycle through the IF statements and blank their field result before the merge (as per the example) … To my mind, all that should be doing is placing the IFs back to their original state.

Kind regards,

James

@JamesMurdoch Yes, you are absolutely right, currently as a workaround you can simple reset IF field result to empty string before executing mail merge, just like you have shown in your code example.

Excellent, thanks again for your help Alexey.

1 Like