Sample

So I was given the task to create a template document for our project, I wanted to make it so that a lot of the pieces of text are automatically synchronized. By that I mean that repeated parts of text like the version of the document, the name of our project group and so on will always be the same throughout the whole document, wherever you might change it.

 

I knew of insert -> quick parts -> document property, which are kept in sync automatically. but the problem is, these are predefined and you can rename them, but if you rename Status to Version, and you place another Status and rename it to Project name, they are still referring to the same piece of text. So Version and Project name will still refer to the initial Status text. I definitely didn't want this! so I started to look up if I could add my own set of these text fields. These document property fields actually use XML, and you can import your own XML to a Word file! So let's take a look at a very basic implementation.

 

Let's first create an XML file.

<Root xmlns="Easy Document management">

  <Company>

    <Name/>

    <Address/>

    <PhoneNumber/>

    <FaxNumber/>

    <E-mail/>

    <Contact/>

  </Company>

 

  <Document>

    <Name/>

    <Date/>

    <Status/>

    <Version/>

    <Description/>

    <Subject/>

    <Type/>

  </Document>

</Root>

 

Let me explain a bit about the above, you always need to have a root element, under which all the properties will be nested. in my case this is the 

<Root xmlns="Easy Document management">

part. The xmlns="Easy Document management" part is an XML Namespace, This will be the title for the XML file in Word.

 

Under the Root element, I created 2 new elements (Company and Document), and under those subjects I can place a lot of the properties I wanted to add to the document. As you can see the properties under Company and Document include a "/" after it, this is because all elements in a XML file have to be opened and closed. Usually you write it like <Element></Element> (remember, XML is case sensitive!!), but <Element/> is a self-closing tag, this way is a little shorter/faster and maybe even cleaner. As you can see the Company is opened and closed, and has all properties in between that are (in my case) related to the company. You don‘t have to do this, but for me it’s more convenient. So at the moment we have a valid XML file that we can import to Word (2010 and above).

 

Let’s open Word and navigate to the developers tab. If you don’t have the developers tab, click on File -> Options -> customize ribbon -> in the right pane check the box Developer and close the options menu. In the developer tab press “XML-mapping pane”. At the right you’ll see a new pane, click on the dropdown menu (Figure 1) and choose “(add new part...)” (Figure 2) and navigate to the XML file. You’ll see your data in a nice tree view (if you created nested elements in your XML data file) (Figure 3), if you already filled in some data in the XML itself, these will be automatically filled when you add them. To add one of these fields, right click the one you want to insert and choose what type of field it should be, Text without markup, Date or an image (sadly, images won’t synchronise).

Figure 1 Figure 2

Figure 1

Figure 2 Figure 3

 

Now when you later see that you forgot something in your XML file, there isn’t a nice edit button… You could also add a new XML file, but this isn’t that nice (I think).

Remember that this might be risky, so always make sure you have a backup before you try the following!

To change the XML file inside the document, you’ll need to have an Archiving program, I recommend 7-Zip, as it is free and Open Source. Save and close your Word document (and just to be sure create a backup), and open the Word document with your favorite archive tool. Open the folder customXml. In there you’ll see 5 items, 1 folder and 4 files (if you only imported 1 XML file) if you want to edit your XML file, you’ll need to edit item1.xml (so add the missing properties), save the XML, update the archive (so your edited XML get imported bank and close the archive). If you created an invalid XML file, Word will encounter an error, and will ask you to repair it, but most of the times the relationships will break, and not automatically synchronize anymore. When Word says that there’s an error, just press don’t repair and close the file. After that just pull your XML file through an XML validator and repair the problem yourself, this will save a lot of time reading all fields ;) .