When calling MailMerge.Execute followed by MailMerge.ExecuteWithRegions I get an exception.
System.InvalidCastException: Unable to cast object of type 'Aspose.Words.Fields.FieldSeparator' to type 'Aspose.Words.Fields.FieldStart'.
The solution until a fix is supplied is to save the result from first Execute to a stream. Create a second Document from the stream and call ExecuteWithRegions on the second document.
/Uffe
Thanks for your request. Could you please attach your template document and simple code here for testing? I will check the issue on my side and provide you more information.
Best regards,
static void Main(string[] args)
{
var doc = new Document(@"test.template.docx");
doc.MailMerge.Execute(new string[]
{
"Anvandare_Namn"
}, new object[]
{
"username"
});
var ds = new DataSet();
ds.ReadXml(@"datatable.xml");
var dataTable = ds.Tables[0];
doc.MailMerge.RemoveEmptyParagraphs = true;
doc.MailMerge.ExecuteWithRegions(dataTable);
// Exception. Unable to cast object of type 'Aspose.Words.Fields.FieldSeparator' to type 'Aspose.Words.Fields.FieldStart'.
}
Thank you for additional information. I managed to reproduce the problem on my side. You will be notified as soon as it is resolved.
It seems there is something wrong with Styles of merge fields. As a workaround you can try clearing styles of merge fields inside region or you can modify the TOC field. As a temporary workaround, you can try using the following code of TOC field and the problem disappears: TOC \o “1-1” \h \z \t
Best regards,