Programmatically add Database fields list to select Merge Fields


In our system we give ability to provide user to add merge fields in MS Word templates, so that when actual print/email job is done they can be mapped and copies are created. We have a collection of merge fields for the user to select from (around 100 of them).

Is it possible to attach the list of placeholders(merge fields) to the document so that once the user download the Word doc and select the merge field to be added to the template programmatically?

Something like attached screenshots. Either in Database Fields or Merge Fields.
InsertField-MergeField.png (51.7 KB)
DataBaseFields-MailMerge.png (66.9 KB)


@parthiban.natarajan You can achieve this by specifying specify a mail merge data source for a document and this information (along with the available data fields) will appear in Microsoft Word when the user opens this document. See Document.MailMergeSettings for more information.

Thank you for you prompt response. This solution seems working and we have encountered few observations.

  1. It gives Mail merge popups on opening the screenshots(see attached). Can that we avoided or suppressed?
  2. Mailmerge fields are not visible unless we open the Mail Merge wizard and go to step 4 “Arrange your labels”. We have empty datasource with columns and blank values. Is there a way so that as soon as user insert the field, its visible.
  3. Fields come with underscore instead of space. Any possibility to retain spaces?
    MailMerge.png (37.7 KB)

Thanks in Advance.


  1. Unfortunately, there is no way to control this using Aspose.Words. This is by design behavior of MS Word.
  2. I used the following code to generate document and field names are available immediately after the mentioned prompts are accepted:
// Create a data source in the form of an ASCII file, with the "|" character
// acting as the delimiter that separates columns. The first line contains the three columns' names,
// and each subsequent line is a row with their respective values.
string[] lines = { "FirstName|LastName|Message" };
string dataSrcFilename = @"C:\Temp\MailMerge.MailMergeSettings.DataSource.txt";

File.WriteAllLines(dataSrcFilename, lines);

MailMergeSettings settings = doc.MailMergeSettings;
settings.MainDocumentType = MailMergeMainDocumentType.MailingLabels;
settings.CheckErrors = MailMergeCheckErrors.Simulate;
settings.DataType = MailMergeDataType.Native;
settings.DataSource = dataSrcFilename;
settings.Query = "SELECT * FROM " + doc.MailMergeSettings.DataSource;

Odso odso = settings.Odso;
odso.DataSource = dataSrcFilename;
odso.DataSourceType = OdsoDataSourceType.Text;
odso.ColumnDelimiter = '|';
odso.FirstRowContainsColumnNames = true;

// Opening this document in Microsoft Word will execute the mail merge before displaying the contents. 
  1. Unfortunately, I do not see any option to retain spaced in merge field names when use an attached data source.