Microsoft optimize

Use Word's StyleRef field to efficiently reuse text

Use Word's STYLEREF field to create dynamic links that more efficiently reuse text.

Repeating content is common. For instance, you might want to repeat a name or refer to a document's title in the header. You could re-enter the text each time you want it to appear in the document. An AutoComplete item would reduce your input effort, but either way, you'd still have to remember to update each instance if you change the original text. For example, let's suppose you want to display a document's title and major headings in the document's header. If you change the actual title or heading text later, you'll have to remember to also update the headers. That type of task is hard to remember, especially months after the fact. Wouldn't it be great if you could link the header to the original title and headings? That's exactly what Word's StyleRef field does.

About StyleRef

In a nutshell, the StyleRef field is a reference field that displays text automatically. When using the field, you'll identify the style you've applied to the text you want to repeat in the form

{STYLEREF "stylename" [switches]}

You must delimit stylename as a string using the quotation marks and switches is optional. Word searches for the closest text formatted with stylename and then displays that text. This field supports the following switches:

  • \l – Inserts the last text formatted appropriately on the current page, searching from bottom to top. (The default is to search from top to bottom.)
  • \n – Inserts the paragraph number from the referenced text.
  • \p – Inserts the relative position of the referenced text.
  • \r – Inserts the paragraph number of the referenced text in relative context.
  • \t – Suppresses all non-limiter characters.
  • \w – Inserts the paragraph number of the referenced text in full context.

There are two ways to insert fields. I'll show you both and you can choose the method you prefer. To use the interface, do the following:

  1. Position the insertion point where you want to display the repeated text.
  2. Click the Insert tab. In the Text group, choose Field from the Quick Parts dropdown. Word 2003 users will find the Field option on the Insert menu.
  3. Select StyleRef from the Categories list.
  4. From the Style Name list, select the style you've applied to the text you're repeating.
  5. Check the Field Properties (switches) that apply. (They all correspond to one of the switches listed above.)
  6. Click OK.

You can bypass the interface and enter field codes directly as follows:

  1. Position the insertion point where you want to display the repeated text.
  2. Press [Ctrl]+[F9]. Word will display the field code brackets ({}).
  3. Press [shift]+[F9] to convert the field's code to its resulting text.

Title

An example Word doc illustrating this technique is available for download.

As you can see below, there are four styles in use in our sample document: Title, Heading 1, Heading 2, and Normal. To display the text formatted using the Title style, do the following:

  1. Open the header on page 2 by double-clicking that section.
  2. Using either method outlined above, enter the following field code: {StyleRef "Title"}. Don't try to copy it from this web page - that won't work. If you use the [Ctrl]+[F9] method, remember to press [Shift]+[F9] to complete the process.

This field code, as is, will display that text in the header until you apply the Title style to new text - so don't do that unless that's what you intend to do. Using this technique does require that you limit your uses of particular styles, but in most cases, that won't be a problem. When it is, create a second style based on the first and use it for text you want to display the same way within the document, but don't want interpreted by the StyleRef field code.

Heading 1

Now you're ready to display heading text (Heading 1). Unlike the document's title on page one, you will probably apply this style multiple times throughout the document. The StyleRef field will update accordingly. Using your preferred method, enter the following field under the Title field code in the header: {StyleRef "Heading 1"}. Remember to press [Shift]+[F9] to update it.

The field will continue to display "Using Galleries" until you apply the Heading 1 style again. The next few figures show what happens when you change header styles:

  • When there's no appropriately styled text on the current page, Word searches the previous pages.
  • Adding a Heading 1 style closer to the field will update the field's display text.
  • If there's no Heading 1 text, the field returns an error.
  • By default, Word begins at the top of the current page. To change that, add the \l switch. Doing so will return the styled text closest to the bottom of the page.

Rather than displaying both title and headings on every page, you can use the Odd Page Header and Even Page Header options to display one on even pages and the other on odd pages!

About

Susan Sales Harkins is an IT consultant, specializing in desktop solutions. Previously, she was editor in chief for The Cobb Group, the world's largest publisher of technical journals.

3 comments
Cal Wilson
Cal Wilson

@basil.cinnamon Thanks for your suggestion. It's interesting. However, it doesn't solve the problem of updating text changes, i.e., updating changes automatically. When the bookmark text is modified, you have to update the cross-reference as it is also a field. The cross-reference doesn't update automatically.

basil.cinnamon
basil.cinnamon

Select the source text, insert a bookmark, then insert a cross-reference to the bookmark text anywhere else in the document. You avoid the Styleref issue of pointing to the last style, which is not a robust solution, as described. You can also insert multiple crossrefs to the same bookmark anywhere in the doc.

Cal Wilson
Cal Wilson

I use StyleRef fields often in the header (and footer) as Susan explained. I also use them in body text often. I have found some strange behavior. I wonder if I am doing something wrong. When the text related to a StyleRef field in a header/footer is updated, the field updates immediately. When the field is in the body text, I have to force an update, for example, press Ctrl+A, F9 to update all fields in a document. This is not a large problem for me, but it is a major training issue when I am teaching others how to use a template containing StyleRef fields. Typically, others will forget the steps and need for updating fields in the body text. Any idea how to make StyleRef fields in the body update immediately without extra work as they do in the header/footer? Thanks