3

In our document generation system we use DOCX files in which we programmatically fill out mergefields. For this I'm using the OpenXml SDK 2.0.

I've been plowing through the document.xml file, in de docx, and found that the mergefields are usually represented by a SimpleField. An example from a document we use:

<w:fldSimple w:instr=" MERGEFIELD  NP021_INSSNumber  \* MERGEFORMAT "><w:r><w:rPr><w:noProof/></w:rPr><w:t>«NP021_INSSNumber»</w:t></w:r></w:fldSimple>

A fairly straightforward notation, containing the mergefield command and the text to be displayed in the document. It's fairly easy to find this tag in the XML, just search for w:fldSimple tags. (I removed some style tags to make it more readable)

But a document recently created in Word didn't parse in our code, and when I looked in the XML the notation for mergefields was completely different:

<w:instrText xml:space="preserve"> MERGEFIELD  NP021_INSSNumber  \* MERGEFORMAT </w:instrText>

And later in the document I found the display notation: <w:t>«NP021_INSSNumber»</w:t> This is spectacularly impossible to parse in code.

How is it possible that doing the same thing in Word can have such different results, and is there a way to ensure that Word uses SimpleFields as XML notation for mergefields?

Thank you in advance for any helpful input.

Joachim VR
  • 2,320
  • 1
  • 15
  • 24
  • I am seeing the same issue and trying the RevisionAcceptor and simplifying markup doesnt fix it. My merge fields are still staying as a instrText and not being converted back to fldSimple. Has anyone else seen this before? How did you resolve this issue? – Lav Aug 31 '12 at 14:52
  • Looks like both are supported - these docs (http://officeopenxml.com/WPfields.php) distinguishes between simple and complex fields – David Basalla Apr 16 '21 at 07:43

1 Answers1

2

I would consider accepting revisions and simplifying the markup prior to parsing it.

Note that the MarkupSimplifier is included in the Power Tools for Open XML.

You will probably find lots more useful material in Eric Whites blog postings.

Scott Munro
  • 13,369
  • 3
  • 74
  • 80
  • I am seeing the same issue and trying the RevisionAcceptor and simplifying markup doesnt fix it. My merge fields are still staying as a instrText and not being converted back to fldSimple. Has anyone else seen this before? – Lav Aug 31 '12 at 14:52