Word 2007, Content Controls, Databinding and Schema Validation


I've been trying to help someone out recently who'd built themselves a nice little add-in for Word 2007 to streamline the process of adding, binding and validating content controls. It stuck me that few people know about these features and even fewer have gone to the trouble of documenting any of them. In this post, I'd like to introduce the overall capability and give a simple end to end example of how it's done.

The components are:

  • Word 2007 - everyone's favourite word processing application
  • Content controls - a new feature in Word 2007. Do lots of clever things including assisting with layout, restricting content and their party trick; databinding to XML data in the custom XML store (part of an Office Open XML document)
  • XML schema validation in Word is nothing new but you can associate a schema to validate your databound content controls and get visual feedback where elements don't comply

To see this in action I've created an XML document (from an MSDN sample) and an associated schema.

Document

<?xml version="1.0"?>
<catalog xmlns="http://mikeo.co.uk">
    <book id="bk101">
        <author>Gambardella, Matthew</author>
        <title>XML Developer's Guide</title>
        <genre>Computer</genre>
        <price>44.95</price>
        <publish_date>2000-10-01</publish_date>
        <description>An in-depth look at creating applications with XML.</description>
    </book>
</catalog>

Schema

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" 
                     elementFormDefault="qualified" 
                     xmlns:xs="http://www.w3.org/2001/XMLSchema"
                     targetNamespace="http://mikeo.co.uk">
    <xs:element name="catalog">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="book">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="author" type="xs:string" />
                            <xs:element name="title" type="xs:string" />
                            <xs:element name="genre" type="xs:string" />
                            <xs:element name="price" type="xs:decimal" />
                            <xs:element name="publish_date" type="xs:date" />
                            <xs:element name="description" type="xs:string" />
                        </xs:sequence>
                        <xs:attribute name="id" use="required">
                            <xs:simpleType>
                                <xs:restriction base="xs:string">
                                    <xs:pattern value="[a-z]{2}[0-9]{3}"></xs:pattern>
                                </xs:restriction>
                            </xs:simpleType>
                        </xs:attribute>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

I want to build a Word document that uses content controls mapped to elements in my XML document validated by the schema. I start by adding the content controls to my Word document. Content controls are added from the Developer Tab -> Controls group (if the Developer Tab is not showing, go to Word Options -> Popular -> Show Developer Tab in Ribbon). I've added 5 plain text control, 1 drop-down list and 1 date picker.

image

Next I need to create the custom XML part for my document and create the mapping for each content control so it will automatically databind to the respective element in the custom XML part (the controls support two-way databinding so if you update the XML the control will update and vice-versa). The easiest way to do this is with the Word 2007 Content Control Toolkit.

Open the document in the W2CCT and create a new custom XML part. image

Then switch to "Edit View" in the custom XML parts area and paste in your XML.

image

Then switch to "Bind View" and map each node to the corresponding content control by dragging and dropping from the Bind View to the Content Controls view.

image

You'll see the XPath expressions update as you do this. Save the document and close W2CCT. Open the document again in Word and you'll see there's data in the content controls as they are now databound to the custom XML part.

image

Finally, we want to enable our schema for validation purposes. Right now, there is no validation so I can type anything I like in the Price and ID fields.

image

To enable validation against our schema, from the Developer tab select Schema and Add Schema then add the schema file to validate against.

image

You'll probably find you have to close Word and re-open and you should then have schema validation enabled. Note the red outlines on the invalid content controls and the tooltip assisting me in entering a valid value. These are automatically generated by Word based on the schema we have associated.

 image

Comments (2)

  1. Greg Maxey says:

    Mike,

    Nice article!

    You are welcome to share the link to my AddIn:

    http://gregmaxey.mvps.org/Mapped_Content_Control.htm

Skip to main content