We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to read mail merge fields?

I have create a document using MailMerge. I want to read the values of all the fieldsin the document that have created using MailMerge. How do ido that?

Hi

Thanks for your request. Yes, you can, but there is no direct way. You can use the same approach as used here:

http://docs.aspose.com/display/wordsnet/How+to++Rename+Merge+Fields


Hope this will help. In case of any ambiguity, please let us know.


Hi there,


Thanks for your inquiry.

You can use the MailMerge.GetFieldNames method. Please see the following page for details: http://docs.aspose.com/display/wordsnet/How+to++Use+Advanced+Mail+Merge+Features

Thanks

I just wanted to be more clear on my question. I am trying to read the content of the mail merge field after execution. Please see the code and comments below

Document doc = new Document(templateStream);
doc.MailMerge.Execute(dataTable)
doc.Save(outputStream,SaveFormat.Docx)


// load just created document
Document doc = new Document(outputStream);

// Now here i want read the merge field name & its content
// i'm not sure document will still have the merge fields after execution

Hi there,


Thanks for your inquiry.

No, I’m afraid there is no direct way to do that. The merge fields are gone as soon as your execute mail merge.

Instead you may want to wrap the merge fields with bookmarks before merging, then after reloading your document you can find the bookmark and get the original field name from the bookmark name.

Please see the code below for an example of this.

<span style=“font-size:
10.0pt;font-family:“Courier New”;color:#2B91AF;mso-no-proof:yes”>DocumentBuilder<span style=“font-size:10.0pt;font-family:“Courier New”;mso-no-proof:yes”> builder = new DocumentBuilder(doc);<o:p></o:p>

foreach (string fieldName in doc.MailMerge.GetFieldNames())

{

builder.MoveToMergeField(fieldName, false, false);

builder.StartBookmark(fieldName);

builder.MoveToMergeField(fieldName, true, false);

builder.EndBookmark(fieldName);

}

doc.MailMerge.ExecuteWithRegions(dataSet);

foreach (Bookmark bookmark in doc.Range.Bookmarks)

{

// Retrieve the field name and the merged field value using the bookmark.

string fieldName = bookmark.Name;

string fieldValue = bookmark.Text;

}


Thanks,