Hide empty SharePoint metadata columns in Word 2010

Scenario: A Microsoft Word document is created in a SharePoint library that uses custom content types. The content type for a specific Word document has some metadata columns to add information about each document. The metadata columns are not mandatory, so they may be empty.

Inside the Word document, the metadata columns can be inserted via the ribbon Insert > Quick Parts > Document Property and can then be used to dissplay the document’s metadata inside the Word document.

This could be information like document status, a project code, a target delivery date or any other kind of value. The metadata could be used to trigger the display of specific paragraphs, depending on the column’s value.

If these metadata columns are used in the Word document to display the metadata itself, then the empty columns will show as editable document properties. The user can write the values straight into the document properties as they appear within the Word document and the data will be written back to SharePoint when the document is closed.

This works fine so far.

There are situations, though, where you may not want the user to edit the empty document properties in the Word document. Any metadata changes may be taken care of with workflows in SharePoint, for example, and the Document Properties entered via Quick Parts may only be used to display the data from the metadata columns, or trigger the display of specific text in the Word document.

If a document property is inserted in the Word document via Insert > Quick Parts > Document Property, but it does not contain a value, it will still show in the Word document, though. In fact, it will look rather ugly. The property name is shown in square brackets with a light grey font and it will also show when printing the file.

This screenshot shows a part of the Document Information Panel and some of its document properties inserted into the Word document.

The goal now is to hide these fields in the Word document, unless they contain values.

Word has a rich repertoire of field codes to manipulate data on the fly. The document property can be wrapped into an IF field, for example,  to make it display only when it is not empty.

If you have used Word 2003 or earlier field codes before, you may be familiar with the syntax:

{ IF {FieldName} = "" "" "{FieldName}"}

Translation: If the field called “FieldName” is empty, i.e. it shows as a blank string, which is represented with two double quotes with nothing in between, then do nothing (the second set of double quotes). Else, i.e. if the field is not empty, return the field.

Trying the same approach on a Word 2010 document property fails miserably, though. Using this …

{ IF [Company Fax] = "" "" "[Company Fax]"}

… will never hide the field. (I’ve marked the document properties inserted via the Quickparts menu in blue, to distinguish them from typed text). Although the field is empty, i.e. it does not have a value, it will not be hidden in the Word document.

As mentioned above, an empty field code will still display a faint gray text in the document. Even if the actual property does not contain any value, the item in the Word document will show as “[FieldName]”

That is the crucial part when building the comparison statement of the IF function in the field code: When building the comparison statement, you need to take into account what is displayed in the Word document, rather than what is actually stored in the field.

So, even if the field is empty, Word displays [FieldName]. Therefore, if you want to find out if a field is empty, you need to compare it to “[FieldName]” as  a text string, rather than to an empty text string like “”.  In fact, that even extends to situations where the field is not even displayed in the document. If you want to conditionally show a certain text, based on a field value (or the field being blank), you will still need to use the “[FieldName]” as the comparison to a blank field.

Makes perfect sense, right?

Step by step:

  1. In Word 2010, use Alt-F9 to display field codes (hold down the Alt key and then hit F9)
  2. Click the place in the document where you want the document property to go.
  3. Use Ctrl-F9 to enter a new, empty field code. It will show as a set of curly brackets {}
  4. Click between the curly brackets and use Insert > Quick Parts > Document Properties. Select the desired document property.
  5. Click after the double quotes, enter a = sign and then add a pair of double quotes.
  6. Click between the two double quotes and type the name of the document property, including the square brackets.
  7. Click after the double quotes, enter a space and two double quotes. This is the “true” statement of the IF function. If the condition is true, this empty string will be returned as the result.
  8. Add another space and another set of two double quotes for the “false” statement, which will fire if the condition is not true, i.e. the field contains a value.
  9. Click between the last two double quotes and use Insert > Quick Parts > Document Properties. Select the same document property.

The result should look like this (I’ve used the document property “Company Fax”

{ IF [Company Fax] = "[Company Fax]" "" "[Company Fax]"}

Now use Alt-F9 to hide the field codes again, select the text that contains the field code and hit F9 to refresh.

The document property will now only display if it is not empty.

Note that Word field codes don’t refresh dynamically. If the underlying data has changed, the fields need to be recalculated by selecting the text and hitting F9. Most field codes will be refreshed automatically when a document is opened, or saved, but there are exceptions (headers, footers, tables). As far as I’m aware, all field codes will be refreshed automatically before printing.

Watch this video for an run-through illustrating the concept with the Properties in Microsoft Word 2003 and the Document Properties in Microsoft Word 2010.

Microsoft SharePoint metadata can serve as a trigger for very powerful manipulation of a Word document on the fly, but the devil is in the detail when it comes to evaluating if a field is empty.

One comment

Comments are closed.