Free Support Forum -

How can I get the value of a MergeField once merged?

I have a base document with a number of mergefields. When I merge the data into the document to obtain a new document I mantain the fields in the document.

I accomplished this by using a custom class implementing IFieldMergingCallback interface, with a function like this one:

Private Sub IFieldMergingCallback_FieldMerging(ByVal args As FieldMergingArgs) Implements IFieldMergingCallback.FieldMerging
Dim builder As New DocumentBuilder(args.Document)
builder.MoveToField(args.Field, True)
builder.InsertField(args.Field.GetFieldCode().ToString(), args.FieldValue.ToString())
args.Text = “”
End Sub

Once I saved this document another process will need to open the document and retrieve the values of certain fields.

How can I accomplish this?

Hi Joan,

Thanks for your inquiry. In your case, you can get the fields values from your data source. However, you can also get field values by using FieldMergingArgs.FieldValue property. You may also add field names and values into Hashtable in IFieldMergingCallback.FieldMerging and use this Hashtable into your application.

Hope this answers your query. Please let us know if you have any more queries.

Thanks for the answer, but what I need is not exactly what you proposed.

I need to recover the field values once the merging is completed and the application closed.

Once the process is completed another application will load the resultant document (the one I attach to this post) and this is the application that needs to recover the filed value. In this application I will not be doing any merge, what I need is a way to recover the mergefields of a document once merged and the values assigned to each one.

In short, what I am interested in is loading the attached document, retrieve all mergefields a the values assigned to each one.

Hi Joan,

Thanks for sharing the details. You can get Merged Field values by using Field.Result property. This property gets or sets text that is between the field separator and field end.

Please note that Aspose.Words tries to mimic the same behavior as MS Word do. In your case, if you update the fields by using MS Word or Aspose.Words the the field’s value become as <>. The first Mail Merge field of your document is name (MERGEFIELD Name * MERGEFORMAT). After updating the fields, it will become from ‘John’ to «Name».

However, you can get field’s results before updating fields by using following code snippet. In your case, please do not call the Document.UpdateFields. You may call this method after getting the values of fields.

<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

  • “TestDocument.docx”);<o:p></o:p>

DocumentBuilder builder = new DocumentBuilder(doc);

//doc.UpdateFields(); // do not call this method before getting values

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


builder.MoveToMergeField(fieldname, false, false);

// The builder cursor should be positioned at the start of the field.

FieldStart fStart = (FieldStart)builder.CurrentNode;

Console.WriteLine(fieldname + " => " + fStart.GetField().Result);


//doc.UpdateFields(); // you may call this method after getting field values

Moreover, a field in a Word document is a complex structure consisting of multiple nodes that include field start, field code, field separator, field result and field end. Fields can be nested, contain rich content and span multiple paragraphs or sections in a document. The Field class is a "facade" object that provides properties and methods that allow to work with a field as a single object.

The Start, Separator and End properties point to the field start, separator and end nodes of the field respectively. The content between the field start and separator is the field code. The content between the field separator and field end is the field result. The field code typically consists of one or more Run objects that specify instructions. The processing application is expected to execute the field code to calculate the field result.

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