Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
This page explains how to perform a nested mail merge with regions to generate hierarchical or grouped data layouts.
In some scenarios, you may need to use nested Mail Merge with regions. Nested merge is a feature that enables you to merge hierarchical data from your data source into your merge template to easily populate your document. Basically, the hierarchical data is represented as a set of data items, and hierarchical relationships describe how the data items are related to each other (one item of data is the parent of another one).
Aspose.Words allows you to perform a Mail Merge operation with nested regions. You can use this feature if you have a data source that is organized into a tree‑like structure and you want to execute a Mail Merge operation to populate a template with hierarchical data.
The Mail Merge region is called nested if you have two or more Mail Merge regions where one of them is inside the other in a hierarchical form. Note that each region contains data from one table.
The most common example of a nested Mail Merge is an order that contains multiple items where you need to link multiple data tables and present the information in a template.
The picture below shows two nested regions where the Order Mail Merge region is the parent of the Item Mail Merge region.
The data to be merged into a template can come from various sources, mainly relational databases or XML documents. In our example, we are going to use an XML file to store our data and read it with a custom data source.
The custom data source uses the structure of the XML to relate each Order to its child Item elements.
The image below shows how the data from the Order table passed to the nested merge regions will be linked to the Item table, as well as the output generated during the merge operation.
As you can see from the output document, each order from the Order table is inserted into the merge template with all order’s related items from the Item table. The next order will be inserted along with their items until all the orders and items are listed. The order of nesting Mail Merge with regions in the template must match the data relationships between the tables in the data source.
The following code example shows how to generate an invoice using nested Mail Merge with regions:
You can download the order data and the invoice template from Aspose.Words GitHub.
You can also see How to Create Data Relations from a Custom Data Source.
You need to set up all data relationships in the parent‑child structure to execute the nested Mail Merge with regions correctly. Skipping this important step can lead to a failure in executing the nested Mail Merge with regions.
When retrieving data for a nested Mail Merge from XML, select the child records under the current parent element. If the merge is not working as expected, verify that the XML structure matches the regions in the template.
The following code example shows how the invoice data source returns the items for the current order.
Implement the IMailMergeDataSource interface to create relationships in the parent‑child structure of your custom data source. Use the GetChildDataSource method to return the relevant child data of a current parent record.
The following example shows how to create data relations using GetChildDataSource:
Q: How do I implement IMailMergeDataSource for a parent table in C++?
A: Create a class that inherits from Aspose::Words::MailMerging::IMailMergeDataSource. Implement get_TableName, GetValue, MoveNext, and GetChildDataSource. In GetChildDataSource, return a new instance of a child‑data‑source class that represents the related table rows.
Q: How does GetChildDataSource link parent and child records?
A: When the mail‑merge engine encounters the start of a nested region, it calls GetChildDataSource on the current IMailMergeDataSource. Your implementation should return a data source that enumerates only the child rows that belong to the current parent record, typically by filtering on a foreign‑key value.
Q: Can I use a DataTable or SQLite query directly without a custom data source?
A: The C++ ExecuteWithRegions overloads accept IMailMergeDataSource and IMailMergeDataSourceRoot. Wrap the result set in a custom data source. Using SQLiteCpp, execute a query for the parent table, then for each parent row create a child data source that runs a second query filtered by the parent’s key.
Q: What are common errors when performing nested mail merge with regions in C++?
A: Mail Merge matches the data source name to the region name without regard to case. When implementing GetChildDataSource, check how your own code compares region names. Return a fresh child source for the current parent’s records and nullptr for an unknown table.
Q: How can I include images or other fields inside a nested region?
A: In the template, put an image merge field (MERGEFIELD Image:ImageField) inside the nested region. Implement IFieldMergingCallback::ImageFieldMerging and pass the image of the current child record with ImageFieldMergingArgs::set_ImageStream or ImageFieldMergingArgs::set_ImageFileName. During mail merge, Aspose.Words replaces the field with that image for each child record.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.