Merge field being read incorrectly

Hi Team,

I’m having some issues when trying to get all the merge field codes of a word document. Initially, every merge field is being read fine but when I add some changes like adding a new merge field or even just plain text, suddenly there are merge fields that are being read incompletely.

See below example:
vpdelbug.PNG (519.2 KB)

On the left list box you can see that all merge field are being read completely and have the \* MERGEFORMAT end tag.
But on the right list box, there are incomplete merge field with no \* MERGEFORMAT end tag.
The only difference between the two document is that I’ve added an extra line with a merge field.

This is the code I use to read the merge fields:
vpdelbug1.PNG (11.6 KB)

I’ve attached both documents in question:
Test1 - Working.zip (466.5 KB)
Test2- Broken.zip (467.5 KB)

Please advise
Thanks

@valuepro,

Thanks for your inquiry. 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. 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.

Please use Field.GetFieldCode method as shown below to get the desired output.

Document doc = new Document(MyDir + "Test2- Broken.docx");
foreach (Field field in doc.Range.Fields)
{
    if (field.Type == FieldType.FieldMergeField)
        Console.WriteLine(field.GetFieldCode());
}
doc.Save(MyDir + "17.11.docx");