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:
https://docs.aspose.com/words/net/customize-field-properties/

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: https://docs.aspose.com/words/net/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.

DocumentBuilder builder = new DocumentBuilder(doc);

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,