2 Comments

This post describes how you can create a Microsoft Office Word (2007/2010/2013) document by merging a Word template and a custom xml document.

 

Note: This post uses Microsoft Office Word 2013, but the same applies to Microsoft Office Word 2007 and 2010.

 

 

Open Microsoft Office Word 2013 open the developer tab on the ribbon

clip_image001

 

 

If the developer tab is not visible, go to File > Options

clip_image002

 

Go to Customize Ribbon > Enable "Developer" tab

clip_image003

 

First insert some text, a table and an image to give the template some body.

Now insert a Plain Text Content Control

clip_image004

 

Click on Properties and enter as:

Title: "CustomerName"

Tag: "CustomerName"

clip_image005

 

Save the document to "C:\Temp\CustomerInfo.docx" and close Microsoft Office Word

clip_image006

 

Rename the file to "C:\Temp\CustomerInfo.zip"

clip_image007

 

 

Extract the file to "C:\Temp\CustomerInfo"

clip_image008

 

 

Add the folder "C:\Temp\CustomerInfo\customXml"

clip_image009

 

 

Create the files item1.xml and itemProps1.xml and the folder _rels in the folder "C:\Temp\CustomerInfo\customXml"

clip_image010

item1.xml

<root>

<customer>

<name>Customer 1</name>

</customer>

</root>

 

 

itemProps1.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<ds:datastoreItem ds:itemID="{8f93798d-1506-45f2-811e-70f72165a32d}" xmlns:ds="http://schemas.openxmlformats.org/officeDocument/2006/customXml" />

The itemID is just a GUID.

 

 

You can create a new GUID with Microsoft Visual Studio:

clip_image011

clip_image012

 

Create a file item1.xml.rels in the folder "C:\Temp\CustomerInfo\customXml\_rels"

<?xml version="1.0" encoding="utf-8"?>

<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">

<Relationship Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXmlProps" Target="/customXml/itemProps1.xml" Id="Rb8d5386a2f8640f88dad1eff30868bcf" />

</Relationships>

The Id is just a GUID without "-".

Edit the file "C:\Temp\CustomerInfo\word\_rels\document.xml.rels"

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">

<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings" Target="webSettings.xml"/>

<Relationship Id="rId7" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml"/>

<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" Target="settings.xml"/>

<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>

<Relationship Id="rId6" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/glossaryDocument" Target="glossary/document.xml"/>

<Relationship Id="rId5" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable" Target="fontTable.xml"/><Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image1.jpeg"/>

<Relationship Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml" Target="../customXml/item1.xml" Id="Rdfc14bfb0c244ab3" />

</Relationships>

The ID is a GUID without "-".

 

Edit the file "C:\Temp\CustomerInfo\word\document.xml"

Search for "CustomerName" in the file.

clip_image013

Add <w:dataBinding w:xpath="/root/customer[1]/name[1]" w:storeItemID="{8f93798d-1506-45f2-811e-70f72165a32d}" />

just after the <w:sdtPr> containing the CustomerName content control.

 

clip_image014

 

 

Edit the file "C:\Temp\CustomerInfo\[Content_Types].xml"

clip_image015

Now when you zip the contents of the folder "C:\Temp\CustomerInfo" and rename the zip to CustomerInfo2.docx, the CustomerName content control should be automatically filled with the text "Customer 1" from the item1.xml file.

When zipping make sure, you zip the contents of the folder en not the folder itself:

clip_image016

 

These manual steps can be automated by using the tool: http://dbe.codeplex.com/ (Word Content Control Toolkit)

This tool was created for Office 2007, but can be used for Office 2010 and Office 2013.

2 Replies to “Manually create a Word document by binding a custom xml document to a template Word document”

  1. I googled for probably two hours looking for this exact information. Can’t wait to try it! Thanks!

  2. With CodePlex shutting down, Andrew’s comment inspired me to post this. If you are looking for commercial options, check EDocGen (www.edocgen.com) for populating XML into Word template

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts