Working with Word Document Properties, Part 1

Office for Mere Mortals helps people around the world get more from Word, Excel, PowerPoint and Outlook. Delivered once a week. free.


Access Archon #174

 


Introduction

Many of my code samples write data from Access tables or queries to document properties in Word documents.  For this technique to work, you have to add the appropriate doc properties to the Word template, which can be a time-consuming process.  In this article I automate the process of creating Word doc properties to match fields in an Access table or query.  (For information on creating Word doc properties manually, for Word 2000 – 2007, see my White Paper on this topic, which is included in the zip for this article.)

Back in 1999, I wrote an article for Office Watch on creating Word doc properties programmatically.  This technique worked at that time, but some time since then (probably because of a change in Word), I found that if I created doc properties programmatically, they would not always be saved.  Recently, I needed to add a large number of doc properties to about 30 Word templates, to receive Access data, and I really didn’t want to have to create the properties manually, so I revisited the code, and did some experimentation, and found that if I did a dummy edit, and then a Save in code (both are necessary), the newly created doc properties would be saved in the template.

The Copy Doc Properties form in the sample databases (the functionality is the same for the 2002-2003 database and the 2007 database) lets you select a Word template, and a table or query from the database (containing Northwind data), and create matching doc properties in the selected Word template.  When the doc properties exactly match the names of the Access fields, you can write code that iterates through the collection of fields, and writes data to the corresponding doc property in Word, instead of referencing each field by name.

frmCopyDocProperties opens with the Select Word Template button selected:

Figure A.  The Copy Doc Properties form, as initially opened.

Clicking that buttons opens a File Picker dialog (created with the Office FileDialog object) with the default User Templates folder as its initial path.

Figure B.  Selecting a Word template

After a template has been selected, the List Doc Props button is enabled; clicking it creates a new Word document from the selected template, clears the contents of the document, inserts a Word table, and fills it with the names of the template’s current doc properties, if any (if there are no doc properties in the template, it prints a message to that effect):

Figure C.  A list of doc properties in a Word template

If you select a table or query in the Select Table or Query combo box, the Copy Doc Props button is enabled; the code first asks if you want to clear any existing doc properties from the template (and does so if you click Yes), then creates doc properties matching the ones in the selected table or query.  Access Yes/No fields are created as Yes/No doc properties; others are created as Text properties, because there are problems with numeric and date type doc properties – values written to numeric doc properties are truncated to whole numbers, and dates lose their formatting, so you may as well write the data to Text doc properties and apply the desired formatting using Word field switches.

After selecting the tmakInvoiceDetails table, clicking the Copy Doc Props button, clicking No to the question about clearing doc properties, doc properties are created in the Contact Letter DP.dot template, and if you then list them you will see both the old and new doc properties are now in the template:

Figure D.  A list of doc properties in a Word template after adding new ones from an Access table

To use this technique in another database, import the frmCopyDocProperties form, and make sure that you have references to the Office, DAO and Word object models (see the References section below).


Supporting Files

The zip file containing this article, in Word 97-2003 format, plus the supporting file(s), may be downloaded from the Access Archon page of my Web site, as accarch174.zip, which is the last entry in the table of Access Archon columns for Access Watch.



















Document Name

Document Type

Place in

Doc Props (AA 174).mdb

Access 2002-2003 database

Wherever you want

Doc Props (AA 174).accdb

Access 2007 database

Wherever you want

White Paper — Adding Custom Doc Properties to a Word Template.doc

Word 97-2003 document

Wherever you want


Want More?

Office Watch has the latest news and tips about Microsoft Office. Independent since 1996. Delivered once a week.