How can I help you?
Nested Mail merge for group
18 Nov 20186 minutes to read
You can perform nested Mail merge with relational or hierarchical data source and independent data tables in a template document.
Mail merge with implicit relational data
You can perform nested Mail merge with implicit relational data objects without any explicit relational commands by using the executeNestedGroup overload method.
Map the field of ancestor group
You can also merge any field in the nested group by mapping the field or column of its ancestor group or table in the data source. To achieve this, you need to add a corresponding group name or table name as a prefix to the merge field name along with “:” separator.
For example:
- The merge field name should be like “TableName:Id” («TableName:MergeFieldName»)
- The merge field name should be like “Image:TableName:Photo” («Image:TableName:MergeFieldName»)
For example, consider that you have a template document as follow.

In the above template, Organizations is the owner group and it has two child groups Departments and Employees. The Supervisor merge field of Departments group is used in Employees group.
The following code example shows how to perform nested Mail merge with the implicit relational data objects.
//Opens the template document.
WordDocument document = new WordDocument("Template.docx", FormatType.Docx);
//Gets the organization details as “IEnumerable” collection.
ListSupport<Organization> organizationList = getOrganizations();
//Creates an instance of “MailMergeDataTable” by specifying mail merge group name and “IEnumerable” collection.
MailMergeDataTable dataTable = new MailMergeDataTable("Organizations", organizationList);
//Performs Mail merge.
document.getMailMerge().executeNestedGroup(dataTable);
//Saves the Word document.
document.save("Sample.docx", FormatType.Docx);
//Closes the document.
document.close();The following code example shows getOrganizations method which is used to get data for mail merge.
public static ListSupport<Organization> getOrganizations() throws Exception
{
//Creates Employee details.
ListSupport<EmployeeDetails> employees = new ListSupport<EmployeeDetails>();
employees.add(new EmployeeDetails("Thomas Hardy", "1001", "05/27/1996"));
employees.add(new EmployeeDetails("Maria Anders", "1002", "04/10/1998"));
//Creates Departments details.
ListSupport<DepartmentDetails> departments = new ListSupport<DepartmentDetails>();
departments.add(new DepartmentDetails("Marketing", "Nancy Davolio", employees));
employees = new ListSupport<EmployeeDetails>();
employees.add(new EmployeeDetails("Elizabeth Lincoln", "1003", "05/15/1996"));
employees.add(new EmployeeDetails("Antonio Moreno", "1004", "04/22/1996"));
departments.add(new DepartmentDetails("Production", "Andrew Fuller", employees));
//Creates organization details.
ListSupport<Organization> organizations = new ListSupport<Organization>();
organizations.add(new Organization("UK Office", "120 Hanover Sq.", "London", "WX1 6LT", "UK", departments));
return organizations;
}The following code example shows Organization, DepartmentDetails, and EmployeeDetails classes.
By executing the above code example, it generates the resultant Word document as follows.
