Free Support Forum - aspose.com

Pull all Mail-Merge Fields from a Word Doc and Store them for Future Processing

Aspose,


I am currently working on a project that will allow a customer to upload Word docs to a known directory, pull the mail-merge fields from the doc, and store the mail-merge fields in a database. I have a process that will kick off at the end of a business day to create mail-merged documents with the mail-merge fields in the database and data from the current day’s processing. This is currently possible with Microsoft’s mail-merge control, and I was curious if you had an example with your mail-merge control.

Thanks in advance!

Rock Runner

Hi Rock,


Thanks for your inquiry. Please use the MailMerge.GetFieldNames method to get collection of mail merge field names available in the document and save these field names to database.

Please use the DocumentBuilder.InsertField to insert a Word field into a document as shown in following code snippet. Moreover, I suggest you please read following documentation link about the usage Mail Merge with Aspose.Words.
http://www.aspose.com/docs/display/wordsnet/Mail+Merge+and+Reporting

<span style=“font-size:
10.0pt;font-family:“Courier New”;color:#2B91AF;mso-no-proof:yes”>Document<span style=“font-size:10.0pt;font-family:“Courier New”;mso-no-proof:yes”> doc = new Document(MyDir

  • “MailMergeSection.doc”);<o:p></o:p>

//returns a collection of mail merge field names available in the document.

string[] fieldnames = doc.MailMerge.GetFieldNames();


//save fields to database

....

....

....

//insert fields to new document

Document doc2 = new Document();

DocumentBuilder builder = new DocumentBuilder(doc2);

foreach (string fname in fieldnames)

{

//Inserts a Word field into a document and updates the field result.

builder.InsertField(@"MERGEFIELD "+fname+" \\* MERGEFORMAT");

builder.Writeln();

}

doc2.Save(MyDir + "out.docx");

Tahir Manzoor,


Thanks for the response and help. Your answer was able to fill my need.

Take care,

Rock Runner

Hi Rock,


Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.