Hi Gavin,
Thanks for considering Aspose.
I have merged both of your threads here as I think they relate to the same topic and can both be answered with one post. Thank you for posting your template. I have created a quick sample for you which shows how your template can be merged with hierarchical data. Please see the code below.
In this case it will merge a field in the header using simple mail merge, and will also merge the main document with some nested data. Only the first few fields from some tables are merged just as an example. I have attached the template and the output document produced after running this code as well.
Document doc = new Document(dataDir + "Audit.Form.General.NoRSS.TEST2.doc");<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
//Create empty dataset used to hold all datatables
DataSet mergeData = new DataSet();
//Create datatables for the main document, make sure the TableName property matches the region in document.
DataTable siteTable = new DataTable();
siteTable.TableName = "Site";
siteTable.Columns.Add("Name");
siteTable.Columns.Add("Location");
mergeData.Tables.Add(siteTable);
//Create child table, including a foreign key column for used for relations.
DataTable activitiesTable = new DataTable();
activitiesTable.TableName = "Activities";
activitiesTable.Columns.Add("ActivityID");
activitiesTable.Columns.Add("PeriodFrom");
//Add the Foreign key
activitiesTable.Columns.Add("SiteName");
mergeData.Tables.Add(activitiesTable);
//Create child table, including a foreign key column for used for relations.
DataTable investigationsTable = new DataTable();
investigationsTable.TableName = "Investigations";
investigationsTable.Columns.Add("ReportDate");
investigationsTable.Columns.Add("ReporterName");
//Add the Foreign key
investigationsTable.Columns.Add("SiteName");
mergeData.Tables.Add(investigationsTable);
//Add some data to the tables
siteTable.Rows.Add(new string[] { "SiteName1", "Location1"});
siteTable.Rows.Add(new string[] { "SiteName2", "Location2" });
//Add child data in activties table for Site1
activitiesTable.Rows.Add(new string[] { "0", "Period1", "SiteName1" });
activitiesTable.Rows.Add(new string[] { "1", "Period2", "SiteName1" });
//Add child data in activties table for Site2
activitiesTable.Rows.Add(new string[] { "0", "Period1", "SiteName2" });
activitiesTable.Rows.Add(new string[] { "1", "Period2", "SiteName2" });
//Add child data in investigations table for Site1
investigationsTable.Rows.Add(new string[] { "Date1", "Name1", "SiteName1" });
investigationsTable.Rows.Add(new string[] { "Date2", "Name2", "SiteName1" });
//Add child data in investigations table for Site2
investigationsTable.Rows.Add(new string[] { "Date1", "Name1", "SiteName2" });
investigationsTable.Rows.Add(new string[] { "Date2", "Name2", "SiteName2" });
//Set relations between the tables, this enables hierarchical (nested) mail merge to work correctly.
mergeData.Relations.Add(siteTable.Columns["Name"], activitiesTable.Columns["SiteName"]);
mergeData.Relations.Add(siteTable.Columns["Name"], investigationsTable.Columns["SiteName"]);
//Execute simple mail merge to merge the field in the header.
doc.MailMerge.Execute(new string[] { "CompanyName"}, new string[] {"CompanyNameTest"});
//Execute mail merge of the main hierarchical data in the document
doc.MailMerge.ExecuteWithRegions(mergeData);
//Save the output to file
doc.Save(dataDir + "Audit.Form.General.NoRSS.TEST2 out.doc");
You can also find detailed information about working with hierarchical (nested) data in mail merge in the nested mail merge documentation here.
If you have any further queries please feel free to ask.
Thanks,