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?
IfFieldExample.zip (23.3 KB)