I am working on an application that uses Microsoft Word files as a template to generate documents. I use Merge fields to get data into my documents.
A SimpleField
can have its result set fairly easy:
SimpleField f = doc.MainDocumentPart.Document.Body.Descendants<SimpleField>().First();
f.RemoveAllChildren();
f.Append(new Run(new Text("somevalue")));
but, when a field is styled in Word, it is save as a FieldChar
, which is a little more complex. The above method doesn't work there, because the result is not a child, but a sibling.
So, basically the question is: how can I set the result of a field (any type) using Open XML SDK ?
The simplest way to do this is with a string replace. Since you have control over the template, put a unique string in the Merge Field. Regardless if the Merge field is formatted or not, the text you need to replace will be in a
Text
element.Then search all Text descendants of the
Body
of theWordprocessingDocument
and do a string replace.Here is a snip of the Fax Merge Document template. I have changed the Business Phone Merge field to «XXXXBusiness PhoneXXXX» and formatted it differently:
I wrote an extension method for the
Body
class to do a replace like so:Once this is in place, your code is a one-liner after you open the document:
The result is seen here:
done!