Free Support Forum - aspose.com

Mail Merge - Issue with IFieldMergingCallBack

I am having an issue when i have a data source that has child tables associated with it. The issue occurs when the parent data source has 2 rows and parent row 1 has zero child rows but parent row 2 has two child rows.

  • Parent Row 1
  • Parent Row 2
    • child row 2

When I get start merging “child row 2” I have some custom code that will insert html into the merge field. It does this with code like this in the FieldMerging callback.

mBuilder.MoveToMergeField(args.FieldName);
mBuilder.InsertHtml(args.FieldValue.ToString());

What happens is the “MoveToMergeField” does not position to the correct point in the document for the child row 2. Instead it will position to the child table under Parent Row 1. And, to make it even more difficult to understand the actual issue, what ends up being inserted at the merge field for child row 2 is the html mark up for the field.

The other thing to note about this scenario is that I have multiple data sources that are being merged into the word template. So the code does not remove unused regions until the last data source is merged into the template. i.e.

doc.MailMerge.CleanupOptions = Aspose.Words.Reporting.MailMergeCleanupOptions.RemoveUnusedRegions;

I believe this is the real source of the problem because the MoveToMergeField statement actually ends up moving to the cursor to the child table under Parent Row 1 instead of the child table under parent row 2.

I have include a sample program that reproduces this issue. Can you help me understand how to properly position to the merge field in this type of scenario.

As a side note, the solution cannot be always have a child row under parent row 1 because I do not have child rows in some scenarios and also I do not want that table region to show up in the document when there are not any child rows for a given parent.





Hi Joe,


Thanks for your inquiry. I am checking with this scenario and will get back to you soon.

Best regards,

Hi Joe,


Thanks for your patience. Well, this is the expected behaviour of Aspose.Words’ mail merge engine i.e. child mail merge regions defined in the document are left intact if there is no data. To overcome this problem you can simply re-arrange the ExecuteWithRegions calls order as follows:

doc.MailMerge.Execute(new
CommonSource());

doc.MailMerge.FieldMergingCallback = new MergeFieldCallBack();

doc.MailMerge.ExecuteWithRegions(new RqmtTraceSource());

doc.MailMerge.CleanupOptions = Aspose.Words.Reporting.MailMergeCleanupOptions.RemoveUnusedRegions;

doc.MailMerge.ExecuteWithRegions(new ProcessSource());


I hope, this helps.

Best regards,