Linking Word 2007 Content Controls to Custom XML

One of the demos I showed in my session at OzFox was inserting some new Custom XML into an existing template file and having that bound to some content controls. This makes mail merges REALLY easy. It also allows you to pick up changes to customer details made in the document itself and propagate them back to the server, because the binding between the Content Controls and the Custom XML is dynamic and bi-directional.

However, doing the binding in the first place is not all that intuitive. Here is a series of steps that worked for me – note that I’m not saying it’s the only, or even the best way.

BTW, there’s a great video up on Channel 9 where Matthew Scott discusses the Word 2007 Content Control Toolkit.

Step 1: Download and install the Word 2007 Content Control Toolkit from CodePlex.

Grab the installer, the documentation and (optionally) the source code from here.

Run the MSI file and accept the defaults.

Step 2: Create a Word 2007 document to use as a template

It doesn’t really matter what’s in it, but you can add some placeholder text using the =rand() function if you like.

Step 3: Add Content Controls to the document

You’ll need to enable the developer tab on the ribbon, then just drag controls from the controls group into the document:

I used text controls (second from the left in the top row), but you can get fancier with Rich Text, images, combo boxes, datetime pickers etc.

Highlight each control and click the properties button in the controls group of the developer toolbar.

Properties button

Give the control a sensible name.

Note that there are a number of other options for the control’s properties. If you want the bi-directional data binding to work, make sure you don’t check the “Remove content control when contents are edited” box.

Do this for as many fields as you want to bind to.

Save and close the document

Step 4: Add some custom XML to the document

Open the document using the Word 2007 Content Control Toolkit.

Word document open in the content control toolkit

Note that at this stage there’s no custom XML in the document. Click the Create a new Custom XML Part hyperlink and change to the Edit View tab and add some XML. You don’t need a schema, it just needs to be well-formed XML. Here’s the XML I used:

<location>Level 9</location>
<street>1 Eagle St</street>

Custom XML inserted 

Step 5: Bind the Content Controls to custom XML nodes

Go back to Bind View and drag nodes from the tree on the right to the appropriate control on the left. Note that you need to select the node before you drag it.

Binding the

Save the document and open it in Word.

Document with data from Custom XML

Now change something (for example change the salutation from “Chuck” to “Charles”). Save and close the file and open it using the Content Control Toolkit. Notice that the XML has been changed.

Comments (4)

  1. As promised attached to this blog post is the Office 2007 UI Extensibility demo code and XML, load it

  2. Jon says:

    Great post. This tutorial + tool helps me bind / map xml nodes to content controls in Word easily. I just wish it was built into Word!

  3. Working on OpenXML? so, how easy it is to map the content controls in word document to custom XML? You

  4. by Lawrence Hodson In this article I have a go at creating an Open XML Document using Microsoft’s excellent…