I’m trying out this Aspose.Words but I’m getting no result. I’m not sure if it’s because my license is temporary or something. First, I’ve prepared a word document with MergeFields in it. Then I created an SQL table with exact field names and I populated it. When I run my test code (see below), all I’m getting is the wordings “Evaluation only…”. Could it be my coding is wrong or is it because my licensing is just temporary. Could you help? I want to make sure that this Aspose.Words will work before I purchase the license. Thanks
class Program
{
static void Main(string[] args)
{
Document doc = new Document("C:\Users\jserrano\Documents\ProductWordLabel.doc");
SqlConnection conn = new SqlConnection("Data Source=COAST6\SQL2005;Initial Catalog=TeamCoast;Integrated Security=true;Persist Security Info=false");
conn.Open();
try
{
SqlCommand cmd = new SqlCommand("SELECT * FROM [ProductWordLabel] ORDER BY PRODNUM", conn);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable tbl = new DataTable("ProductLabel");
adapter.Fill(tbl);
doc.MailMerge.Execute(tbl);
doc.MailMerge.DeleteFields();
string strFile = "C:\Users\jserrano\Documents\ProductWordLabelResult.doc";
FileInfo fi = new FileInfo(strFile);
if (fi.Exists) { fi.Delete(); }
doc.Save(strFile);
}
catch (Exception exc)
{
Console.WriteLine(exc.Message);
}
finally
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
conn.Dispose();
}
Console.WriteLine("Process Finished.");
Console.ReadLine();
}
}
Hello!
Thank you for considering Aspose.
Of course Aspose.Words will work properly. There are no other cases. Let’s see what we have to change.
First of all, you can evaluate the library without evaluation restriction with a temporary 30-day license. Btw, evaluation banner should not affect mail merge and should appear only once at the top. To obtain a temporary license please visit this page: https://purchase.aspose.com/temporary-license
There are two types of mail merge operations: (a) simple mail merge and (b) mail merge with regions. We are doing the first. I expect you have a table at least with the following columns: PRODNUM, PRODDESC, MANUFNUM and INVENMAJCL. And you are trying to achieve the same layout as in the template: two-dimensional array of records in a table. I have tried on a fake data source that returns N records with these names and all works fine. I haven’t changed the template. Can you please check whether your data source provides records as you expect?
Here is my implementation of IMailMergeDataSource:
class MyDataSource : IMailMergeDataSource
{
public MyDataSource(int maxItems)
{
mCounter = 0;
mMaxItems = maxItems;
}
public string TableName
{
get { return "TestTable"; }
}
public bool MoveNext()
{
return ++mCounter <= mMaxItems;
}
public bool GetValue(string fieldName, out object fieldValue)
{
fieldValue = null;
bool result = mCounter <= mMaxItems;
if (result)
{
switch (fieldName.ToUpper())
{
case "PRODNUM":
fieldValue = mCounter;
break;
case "PRODDESC":
fieldValue = "Product No " + mCounter;
break;
case "MANUFNUM":
fieldValue = "Manufacturer No " + mCounter;
break;
case "INVENMAJCL":
fieldValue = "Inventory No " + mCounter;
break;
}
}
return result;
}
private int mCounter;
private readonly int mMaxItems;
}
And here is the function performing mail merge:
private static void TestMailMerge()
{
Document doc = new Document("mail_merge/ProductWordLabel.doc");
MyDataSource ds = new MyDataSource(10);
doc.MailMerge.Execute(ds);
doc.MailMerge.DeleteFields();
doc.Save("mail_merge/ProductWordLabel_result.doc");
}
I did a breakpoint on the doc.MailMerge.Execute(tbl); and did a “Watch” of the tbl.Rows.Count and the DataColumns tbl.Columns[0 thru 3] all correspond to the MergeFields. Is the Execute method supports the System.Data.DataTable? Please see the screenshot below. By the way the DataTable is two dimensional - i.e. it has DataColumns and DataRows.
Hello!
Thank you for your experiments.
There is no magic. DataTable is definitely supported. Otherwise we wouldn’t have such a method. Please try simple mail merge from an array or from a fake data source on your side with minimal changes to your code. Maybe it can shed the light on the reason.
Regards,