When the data source for a mail merge contains column names with spaces in, Microsoft word translates the spaces into underscores “_”.
So if I have a field called “Account Name” then Word will respect and expand the following:
{ MERGEFIELD Account_Name }
However Aspose Words just ignores it.
I can get around this by renaming column names in the Datatable I am passing in, but ideally Aspose.Words would behave as MS Word does.
Hi
Thanks for your reqeust. You do not need to change names of columns in your data source. You can just map between fields in the document and fields in the datasource. Please follow the link for more information:
https://reference.aspose.com/words/net/aspose.words.mailmerging/mappeddatafieldcollection/
Hope this helps. Please feel free to ask in case of any issues, we will be glad to help you.
Best regards.
Hi,
Thanks for the information, but the nature of my application means I can’t do that. I do not know the names of the merge fields or data until run time. The templates are created by end users and they can use different sources for the data.
Although the default behaviour of Aspose.Words differs slightly from MS word I’m happy that I can get around the problem by renaming the columns in the data table.
Nick.
Hi Nick,
Thanks for your request. You also can add mapped fields in runtime. You should just get names of columns in your data source and replace whitespaces with underlines. For instance, see the following code:
// loop through all columns in the data source and add them to mapped data fields collection.
foreach (DataColumn column in data.Columns)
doc.MailMerge.MappedDataFields.Add(column.ColumnName.Replace(" ", "_"), column.ColumnName);
Hope this helps.
Best regards,
Alexey.
Is there any advantage to doing this compared to renaming the source data columns that you can think of?
Thanks again,
Nick.
Hi Nick,
Thanks for your inquiry. You mentioned that your document are created by your users and can use different data sources. So you will need to rename columns in all datasorses that can be used by your users. The code I suggested will work with any data source.
Please let me know if you need more assistance, I will be glad to help you.
Best regards,