Manually create a Word document by binding a custom xml document to a template Word document

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.

Short cut for pasting unformatted text, with the default paste options in Microsoft Office 2010

In most cases when I copy text from a source document or website and want to paste this text in Microsoft Office Outlook or Microsoft Office Word, I want the text to be formatted like the destination document (pasting as unformatted text). Previously I used two methods to accomplish this:

  • Paste > Paste Special > Unformatted Text > OK ( 4 clicks)
  • Notepad ctrl+a then ctrl+v the ctrl+a and then ctrl+c (1 click but many key presses)

Paste special dialog

image

In Microsoft Office 2010 (and perhaps in older Microsoft Office versions) you can now set default paste options to unformatted text. The pasted text will be formatted like the destination document

In Microsoft Office Word 2010

  • File > Options > Advanced > [Cut, copy and paste] section > Keep Text Only

image

In Microsoft Office Outlook 2010

  • Open a new mail message
  • File > Options > Mail > Editor Options > Advanced > [Cut, copy and paste] section > Keep Text Only

image

When you paste text in you’re mail or word document, it will be formatted like the mail or document.

 

Microsoft Office 2010 OneNote

  • In Microsoft Office 2010 OneNote, you can use the [Contenxt Menu Key] + T, instead of [CTRL + V] to paste unformatted text.
  • Or use alt + h + v + t

Microsoft Word 2007 and Squares before every line when "Show paragraph marks" is on

When you open a Microsoft Office Word 2007 document and turn on the "Show paragraph marks" (Ctrl + *), you might find squares before every line. This is because the Paragraph – Line and Page Breaks function Keep with next is turned on.

You can turn this function off by following the steps:
Select the text
Right click on the selected text
Click on Paragraph…
Click on the tab Line and Page Breaks
De-select Keep with next
Click on OK

Data Driven document generation with Microsoft Office Word 2007

Have you ever wanted to create a word document en changing some values in that Word document, by database values entered through a Windows or Web application?

Now you can, with Data Driven document generation with Microsoft Office Word 2007

See:

http://blogs.msdn.com/erikaehrli/archive/2006/08/16/word2007DataDocumentGenerationPart1.aspx

http://blogs.msdn.com/erikaehrli/archive/2006/08/16/word2007DataDocumentGenerationPart2.aspx