Nested Mail Merge


I have seen the documentation and examples on nested mail merging using Aspose Words. It is still unclear to me on how to setup data relations. Can you tell me what might be wrong in the code below. The child table isn’t getting populated while merging and is left as it is. The code im using is something like

PreparedStatement ps = DBUtils.getStatement(con,"Select * from USER_DETAILS");
ResultSet rs = DBUtils.executeQuery(ps);
DataTable table1 = new DataTable(rs, "User");
ps = DBUtils.getStatement(con, "Select ProductName, ProductID, ProductCost from Product");
rs = DBUtils.executeQuery(ps);
DataTable table2 = new DataTable(rs, "Product");
ds.getRelations().add(new DataRelation("UserToProduct", table1, table2, new String[]{"UserName", "UserID", "UserContact"}, new String[]{"ProductName", "ProductID", "ProductCost"}));

I have also attached the sample input and output documents i got.


Hi Siddu,

Thanks for your inquiry. Please read following article for your kind reference. We suggest you please upgrade to latest version of Aspose.Words for Java 16.2.0.

How to Set up Relations for use in Nested Mail Merge with Regions

Please use following highlighted code to setup relation between parent and child tables. The UserID (the primary key of user table) must exists in product’s table as foreign key. Please add UserID in product table. Hope this helps you.

ResultSet userrs = stmt.executeQuery("SELECT * FROM USER_DETAILS");
DataTable table1 = new DataTable(userrs, "User");
ResultSet productrs = stmt.executeQuery("Select * from Product");
DataTable table2 = new DataTable(productrs, "Product");
DataSet dataSet = new DataSet();
DataRelation relation = new DataRelation("UserToProduct", table1, table2, new String[] {"UserID"}, new String[] {"UserID"});
Document doc = new Document(MyDir + "Input.docx");
doc.getMailMerge().executeWithRegions(dataSet); + "Out.docx");

Hi Tahir,

Thanks for the help. Also is it possible to have multiple nested tables for mail merge like

ParentTable --> Child1
–> Child2
–> Child3 --> Child1
–> Child2

If so, how would you determine the data relation for the innermost child tables?


Hi Siddu,

Thanks for your inquiry. Yes, you can use multiple nested tables for mail merge with regions. Please use the same approach to create the relation between parent and child tables as described in following link.

How to Set up Relations for use in Nested Mail Merge with Regions

Please let us know if you have any more queries.