Once the tool is installed (see Installation Instructions for details on the installation process), you can use it to create structured Word documents, using XML parts and mapped content controls.
This introduction will walk you through a simple scenario which demonstrates how the tool can be used.


In this example, we’ll create an XML part that contains all of the header information that we need to extract from a memo template (e.g. the customer’s name), which we could then shred into a SQL database, submit to a web service, etc.

Step 1: Create the XML part

The first step to creating this solution is to define the XML structure of the data we want to capture from the document.
  1. Copy the memo template to your PC: memo.docx.
  2. Copy the following XML file to your PC: data.xml. This file contains the following XML:
    • A memo element, at the root of the XML document
      • A to element to store the customer's name
      • A from element to store the sender's name
      • A subject element to store the subject of the memo
      • A date element to store the date of the memo
      • A CC element, to store any names on the CC: line of the memo
      • A body element, to store the text of the memo itself
  3. Open the memo template in Word 2007 or 2010
  4. Open the XML Mapping task pane by clicking on the XML Mapping button on the Mapping group of the Developer tab
  5. Click on the XML Part drop-down list
  6. Choose Add New Part...
  7. In the dialog box, choose Copy the contents of an existing XML file to create the part, since we already have the XML for the part
  8. Choose Next...'
  9. Choose Browse... and locate the XML file from Step 2 above
  10. Choose Finish
At this point, Word will copy the XML from the file into a new XML part in the document. The original XML file is not deleted, but Word now has its own copy of the data stored in the Open XML document.
The task pane also refreshes to show the XML contents of the new part:
contents of the XML Mapping task pane

Step 2: Insert Mapped Content Controls

Once the XML part has been added to the document, we can now add mapped content controls to the page. Each of these content controls, when edited, will store its text in a node in the XML part.
First, we'll do the header items via right-click:
  1. Place your cursor next to the To: line in the template
  2. Right-click on the to element in the task pane's tree view
  3. Choose Insert Content Control
  4. Choose Text
This will insert a content control mapped to the to element:
Picture of a plain text content control
You can then repeat this for the from, subject and cc elements, inserting text content controls next to each of the corresponding headings.

For the date element, we can insert a date content control:
  1. Place your cursor next to the Date: line in the template
  2. Right-click on the date element in the task pane's tree view
  3. Choose Insert Content Control
  4. Choose Date
Now, the header should have a content control on each line:
Picture of the memo with all of the header content controls
Finally, we can insert a mapped content control for the body element via drag and drop:
  1. Click on the body element
  2. Drag it into the body of the memo (below the dividing line)
  3. Drop it
At this point, all of the data we want to store in our XML part is mapped to content controls in the document.

Step 3: Try it out!

Now that the template is set up, we can try it out:
  1. Save the document
  2. Type content into each of the content controls
  3. Use Save As to save a new copy with your data
  4. Close Word
  5. Rename the file extension to ZIP
  6. In the customXml folder, examine the item1.xml file
This file contains the values of each of the mapped content controls in the document, seperated from the formatting. You can then extract this file to shred into a SQL database, submit to a web service, etc.

Last edited Sep 14, 2011 at 9:52 PM by tristand, version 4


No comments yet.