Free Support Forum - aspose.com

Mappeddatafields question

Hi,

I have a windows form that takes in user details (name, address, etc) and I have several documents that need to be updated with this detail. I would like to find a way to map this form data to the available fields in the various word docs. Can anyone show me an example where this is done…possibly the mappeddatafields method? Also, can anyone think of a good way to persist this mapping in an external file then map dynamically based on the array populated by MailMerge.GetFieldNames()?

thanks!

It is easy to use MappedDataFields:

//Add mappings to the collection like this
doc.MailMerge.MappedDataFields[“DocField1”] = “DataField1”;

//Or like this
doc.MailMerge.MappedDataFields.Add(“DocField2”, “DataField2”);

//Then just execute the mail merge operation that you need
doc.MailMerge.Execute(CreateTestTable());

If you keep your mappings in an XML file, then you can load it and add to MappedDataFields before mail merge, but that’s your code, not Aspose.Word.

Just to make sure this is correct (seems to work):

DataTable dt = new DataTable("Details");

dt.Columns.Add("colName");

dt.Columns.Add("colAddress");

dt.Rows.Add(new object[] {txtName.Text,txtAddress.Text});

doc.MailMerge.MappedDataFields.Add("Name","colName");

doc.MailMerge.MappedDataFields.Add("Address","colAddress");

doc.MailMerge.Execute(dt);

Let me know if there a better way of doing this.

Thanks again.

That looks correct to me.

What I was also talking about is that you can define some configuration or XML file that contains the mappings and you can load that file and add all mappings from it in a loop.