MailMerge.ExecuteWithRegions - document model becomes invalid when merging with regions

Hi,

I’m having trouble with MailMerge.ExecuteWithRegions. It seems that for specific mergefield configurations, the Document.Range.Fields.ToList(); stops working when called from inside FieldMergingCallback through a ExecuteWithRegions. I’m attaching an example project, a cut down version of what we’re using on production but reduced as much as possible while still showing the error.

Some explanation of what is happening in the code:

  • MergeData class - this is not at all what we’re using on production and is just for the example’s sake, I just wanted to use a simple implementation that would allow us to go inside each mergefield and not obscure the parts that were giving us trouble;

  • PrepDocument method - this is very close to what we actually use, we need to have all TableStart and TableEnd tags wrapped in the TableStart:Main and TableEnd:Main tags. This is due to how our actual MergeData works - it has a Main IMailMergeDataSource and all other Tables are represented as children of the Main, so for the mailMerge to correctly navigate the data structure we need to wrap the tables in document with Main so that it looks for the values in child DataSources of Main

  • FieldMergingCallback - It only performs a Document.Range.Fields.ToList(); here, in the actual code it is used to determine if the current field we’re merging into is a child of an IFFIELD in the document, as then we need to sanitize the mergeData for quotation marks to not break the IFFIELD. It seems that only this line is causing issues, and it throws a ‘Invalid document model’ exception.

Please note that the .Range.Fields.ToList(); only starts throwing when performed from within the ExecuteWithRegions method - if I add it inside the Main block it doesn’t throw any exceptions. Could you let me know whether there’s some issue with the document prior to the ExecuteWithRegions that just gets brought to front within the merge method, or if something went wrong when dividing the doc into regions?

Thanks,
Tomek
IfFieldExample.zip (23.3 KB)

@acturisaspose Thank you for reporting the problem to us. I have logged it as WORDSNET-24788. We will keep you posted and let you know once it is resolved or we have more information for you.

@acturisaspose The template document has following structure before mailmerge is performed (simplified):

{ IF 1 = 1 "
{ MERGEFIELD TableStart:Main }{ MERGEFIELD TableStart:Main }
{ MERGEFIELD Field }
{ MERGEFIELD TableEnd:Main }{ MERGEFIELD TableEnd:Main }" "" }

During mail merge Aspose.Words removes region nodes and reproduces (copies and inserts) them for each data source record. Due to internal technical reasons whole paragraphs with region start/end fields and between them are considered as region nodes and are removed from document. As result the topmost IF field loses FieldEnd char and becomes invalid.

You may slightly change document to avoid this problem as a workaround. The paragraph break before IF field end mark fixes the problem Template modified.zip (19.6 KB)