I understand that either it’s my document format or my code that is causing the issue, but I am not able to figure out the reason why MailMerge.ExecuteWithRegions(ds) is not replacing fields in the document
It’s not throwing any error even.
Please find attached document and code file attached.
Note: In AppointmentConfirmation.dotx(attached), I am replacing fields marked with <> to Merge fields at run time.
Thanks for your inquiry. To perform MailMerge your document should contain mergefields, but your document contains only plain text placeholders. Please follow the link to learn how to insert mergefields to a document https://docs.aspose.com/words/net/mail-merge-template/
Best regards,
Attached is the intermediate document that I am getting after replacing fields with Merge Fields, but somehow merge fields are not replaced with actual data found in IMailMergeDataSource.
Thank you for additional information. Why do you execute mail merge with regions? To execute mail merge with regions there should be special merge fields with names TableStart:MyRegion and TableEnt:MyRegion, where MyRegion name of the region.
In your case you should use MailMerge.Execute method instead of MailMerge.ExecuteWithRegions.
Best regards,
Well the document that I sent to you is having a mix of simple Merge fields like {MERGEFIELD streetNumber} as well as
TableStart and TableEnd fields like { MERGEFIELD TableStart:PropertyVendors } and { MERGEFIELD TableEnd:PropertyVendors }
though I am not sure if I am defining the TableStart and TableEnd the right way.
Thank you for additional information. I found the problem in your template. Please try rewriting your mergefield TableStart:PropertyVendors and TableEnd:PropertyVendors without the white space between TableStart: and table name. I attached refactored template here.
Best regards,
I think this works fine becuase there is no parent child relation in the data source for mailmerge.
If I change above data source to following:
System.Data.DataSet ds = new System.Data.DataSet();
System.Data.DataTable propertyData = new System.Data.DataTable("Properties");
propertyData.Columns.Add("PropertyId");
propertyData.Columns.Add("streetNumber");
propertyData.Columns.Add("streetName");
System.Data.DataColumn[] pkCols = new System.Data.DataColumn[1];
pkCols[0] = propertyData.Columns[0];
propertyData.PrimaryKey = pkCols;
// Add row to parent table
propertyData.Rows.Add(new object[]
{
1,
"Street1",
"Amity Street"
});
propertyData.Rows.Add(new object[]
{
2,
"Street2",
"Lodge Street"
});
ds.Tables.Add(propertyData);
// Create child table
System.Data.DataTable data = new System.Data.DataTable("PropertyVendors");
data.Columns.Add("PropertyId");
data.Columns.Add("VTITLE");
data.Columns.Add("VFORENAME");
// Add few rows.
for (int i = 0; i <4; i++)
{
if (i <= 1)
{
data.Rows.Add(new object[]
{
1,
"VTITLE" + i.ToString(),
"VFORENAME" + i.ToString()
});
}
else
{
data.Rows.Add(new object[]
{
2,
"VTITLE" + i.ToString(),
"VFORENAME" + i.ToString()
});
}
}
ds.Tables.Add(data);
System.Data.DataRelation relation = new System.Data.DataRelation("p2c", ds.Tables["Properties"].Columns["PropertyId"], ds.Tables["PropertyVendors"].Columns["PropertyId"]);
ds.Tables["PropertyVendors"].ParentRelations.Add(relation);
// Execute mail merge.
tempDoc.MailMerge.ExecuteWithRegions(ds);
In this case, it just replaces merge fields with the child collection data. I wonder why it’s not merging fields from parent table.
Also, the output document is not in the way it should be - essentially, I should get two page document with same content on each page except
first page to be replaced by data for PropertyId = 1 with it’ s associated child rows
second page with data for PropertyId = 2 with it’ s associated child Vendor rows
Attached is the document I am getting as a result of Mail Merge.
Thank you for additional information. In your dataset, there are two tables, but your template contains only one region. So, when you execute mail merge with regions only one region is filled with data.
Here you can find template and code examples, which show how to fill document with data with parent-child relationships. https://forum.aspose.com/t/81826
Hope this helps.
Best regards,
I was creating my data source in line with the way mentioned in above thread only.
Now, I understand what I was missing in my source document was: { MERGEFIELD TableStart:Properties } at begining of source document and {MERGEFIELD TableEnd:Properties} at end of source document.