Hide those empty SharePoint metadata columns in Word 2010

You may have used Word field codes before to conditionally show document properties in a Word document.

With the new Document Properties that are inserted via Insert > Quick Parts > Document Properties, evaluating if a metadata field is empty is a bit more tricky.

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 display 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.

5 comments

  1. Hi
    I’ve used your method of hiding empty metadata fields and it woks a treat, however… if your metadata has more that one word, it only shows the second word in the field when the IF statement is used, but all data is shown if you dont use the if statement.

    Is the a feature or word? or is there a work around?

    Thanks

    Like

    • Hello,

      As a general rule, every element inside the IF statement that can contain a space character needs to be in quotes. If the metadata field may contain more than one word, i.e. one or more spaces, you need to place the first instance of the document property inside double quotes as well.

      { IF "[MyField]" = "[MyField]" "" "[MyField]"}

      This shorter version will also work:

      { IF "[MyField]" "[MyField]" "[MyField]"}

      The second mention of [MyField] will be text, the other two will be inserted via QuickParts.

      cheers, teylyn

      Like

  2. Hi Teylyn,

    I have a list in SP 2010 that creates a word document when an item is added. That is all good, no problems there. There are a couple of columns though that are not always filled in and therefore no information is populated in the word document. The problem I am having is if the field is empty (I had originally included in the workflow to make the field blank if not completed), it has the red dash line around it and says that the field is “required”. So, I found you article on how to use the IF statement to make the field blank if empty.

    Using the IF, If I populate that field with text, it displays that text. If however, I then remove that text from the field it doesn’t go blank – it keeps the text in the field. If I don’t use the IF, and just insert the Quick Part, it acts as it should and displays the [TEXT] in light grey if empty.

    Hopefully you have seen this behavior and can help. Thanks

    Like

  3. I found a very easy way to do this.

    You just need to open Word Options > Customize Ribbon and select Developer tab. After this you can click Insert > Quick Parts > Document Properties and select your field. You will now see something like this: [Myfield]. Now if you want to hide this and just see empty placeholder, you need to turn on Design Mode on the Developer tab, delete [Myfield] and put a few spaces instead, then turn off Design Mode. After this you will have empty placeholder without any coding.

    Like

Leave a comment