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

Free Support Forum - aspose.com

MailMerge.ExecuteWithRegions IMailMergeDataSourceRoot

Dear Sir/Madame,


I have created a test .docx file containing a single merge field (called FirstField) and a table (demarcated by TableStart:TestTable and TableEnd:TestTable). Within the table I have three columns, themselves merge fields, called ColFieldOne, ColFieldTwo, and ColFieldThree. I call doc.MailMerge.ExecuteWithRegions(//passing my IMailMergeDataSourceRoot).

I have implemented IMailMergeDataSourceRoot and IMailMergeDataSource.
The IMailMergeDataSource implementation is specific to the TestTable mentioned in my template. The TestTable populates properly based on this custom IMailMergeDataSource. Specifically, the methods of my IMailMergeDataSource implementation are called, as they should be, and I provide data to place in the document.

I cannot find a similar mechanism to callback to me to ask for the value of the simple merge field, called FirstField. FirstField is not part of a region. Therefore, I don’t see how your product calls back to ask for its value. The IMailMergeDataSourceRoot::GetDataSource() calls back to me to get the appropriate IMailMergeDataSource, and IMailMergeDataSource::GetValue calls back to me to get the appropriate field value, but nothing calls back to me to ask for the plain old merge field, FirstField, which is not part of a region. How do I set up the callback to occur so that when your parser identifies FirstField, it will ask me for its value.

Edit: I made some further progress. I stopped using IMailMergeDataSourceRoot, and instead wrapped my entire document with TableStart:DocumentData and TableEnd:DocumentData. Then, I could call MailMerge.ExecuteWithRegions(//passing IMailMergeDataSource for DocumentData) - notice I pass the IMailMergeDataSource for the wrapper around the whole document, instead of passing an IMailMergeDataSourceRoot. Then, I can use the GetChildDataSource() method of my IMailMergeDataSource document wrapper to return a one-record table representing the global values, like FirstField, and a multi-record table for TestTable. This works, but it does require me to place the start and end tags in my mail merge document. This is a bad thing since I intend to call in sub-documents, and those sub-documents may already be wrapped in their own start and end tags, which will fail. I can probably work around that as well, but it would be optimal if IMailMergeDataSourceRoot had a method for handling non-region based merge fields.

Edit 2: I discovered IFieldMergingCalback. This allows me to get the callback for the field outside of a region (and for all fields within a region). I no longer need to wrap the document in an outer table. I do, however, need to call MailMerge.ExecuteWithRegions(IMailMergeDataSourceRoot) and then call MailMerge.Execute(new string[] {}, new string[] {}). So, I have to call merge twice. Again, not optimal. It would be nice if I only needed to call it once, but this is better than the solution from the last edit.

Thank you…

Hi Joshua,


Thanks for your interest in Aspose.Words for .NET.

Yes, you need to separately call both ExecuteWithRegions and Execute methods as they do very different things. The MailMerge.ExecuteWithRegions method performs a mail merge operation only on ‘mail merge regions’ inside document and the MailMerge.Execute method performs a mail merge operation on simple merge fields (outside the mail merge regions). So, you have to make two calls. This is by design.

Also, you can implement IFieldMergingCallback interface once to control how data is inserted into merge fields during a mail merge operation (ExecuteWithRegions and Execute methods both can share a single implementation). In the IFieldMergingCallback.FieldMerging method there is a way that you may use to determine whether the current field belongs to any region or not (see MailMergeRegionInfo class).

Please let us know if we can be of any further assistance.

Best regards,