We get an error when we run ExecuteWithRegions with a template where we test on the contens of a field - is it empty or not. This test works fine for all string characters, but if we place a bracket in the text we want to merge, then the code fails with the following error 8beacuse of the empty or not test):
The error is: “Unable to cast object of type ‘❾.⡢’ to type ‘❾.➈’.”
The below small example can reconstruct this problem - Sourcecode + template also attached.
If you change the code line:
drMedie["navn"] = "Publicitas (service intern";
to:
drMedie["navn"] = "Publicitas s(ervice intern";
the test works fine. So our theory is that it must be something with the placement of the bracket that has influense on Aspose’s field type definition. We tried forcing a string conversion by using the * lower option, but that did not help.
We use Aspose.Words.dll vers. 9.1.0.0.
C# example:
namespace AsposeMergeRegionsError
{
class Program
{
static void Main(string[] args)
{
var template = new FileStream("template/template.dot", FileMode.Open, FileAccess.ReadWrite);
var document = new Document(template);
try
{
document.MailMerge.ExecuteWithRegions(GetTestDataTable());
document.Save("template/mergedfile.doc", SaveFormat.Doc);
Console.WriteLine("ExecuteWithRegions completed");
}
catch (InvalidOperationException e)
{
Console.WriteLine("ExecuteWithRegions failed: " + e.Message);
}
}
static DataSet GetTestDataTable()
{
var ds = new DataSet("testdataset");
var dtMedie = new DataTable("mediebureau");
dtMedie.Columns.Add("navn", Type.GetType("System.String"));
var drMedie = dtMedie.NewRow();
drMedie["navn"] = "Publicitas (service intern";
dtMedie.Rows.Add(drMedie);
ds.Tables.Add(dtMedie);
return ds;
}
}
}
Word example:
{ MERGEFIELD TableStart:mediebureau * MERGEFORMAT }
{ IF «navn» <> “” "Navn: " “” }
{ MERGEFIELD TableEnd:mediebureau * MERGEFORMAT }