How to use Word's StyleRef field to customize a header

​Use Word's StyleRef field to repeat content throughout a document, including in the header and footer.

Last month's article How to use prefix tags and VBA to generate conditional content in Word documents showed a simple technique for displaying conditional content. It makes it easy to maintain shared content in a source document, while generating multiple custom documents as needed. This time, we're going to look at a similar solution that repeats content throughout a document using a field. It's not the same problem, but the goal is similar. We all want more efficient ways to maintain content.

I'm using Word 2016 on a Windows 10 64-bit system, but the previous article's code will run in older versions. You can use your own document or download the demonstration .docm or .doc files. The download contains the files for the earlier article and the completed documents for this article. If you want to know how the original document works, read the above linked article first. You can't run VBA code in the browser editions, nor can you use the browser edition to insert a field. However, the browser edition supports fields. You won't see the field's results in the browser, but it will print.

A bit of background

My examples are usually straightforward and simple, but this time, I'm combining the conditional example from last month's article with a repeating content example because together, they provide an interesting real-world scenario. If you'd like to read more about {StyleRef}, see Use Word's StyleRef field to efficiently reuse text.

Word's {StyleRef} field displays text based on the applied style. For instance, you could repeat a chapter's title in the header by applying the Title style to the title content. If you update the chapter's title, the {StyleRef} field in the header will update as well, but it isn't a conditional process.

For this technique, we'll start by adding a conditional subtitle to last month's demonstration file and use that file's macro to display one or the other. Then, we'll use Word's {StyleRef} field to display the resulting subtitle in the document's header. Not only will you learn about {StyleRef}, but you'll learn how to combine two solutions to get the final results you need.

SEE: How to avoid and overcome presentation glitches (free PDF) (TechRepublic)

Add a conditional subtitle

The file shown in Figure A uses prefix tags and VBA to hide text. This is the source document—scavenger hunt questions for students in kindergarten through sixth grade. The VBA procedure solicits the value 1 or 2 and then hides the appropriate questions accordingly. This is the same demonstration file from the earlier article.

Figure A

Our sample document has different questions for two age groups.

Let's suppose you want to add a subtitle that displays the age group. Fortunately, the document is flexible enough to allow this addition. Figure B shows the new age group subtitles and their appropriate prefixes. To see the finished product, run the macro by clicking the Developer tab and then clicking Macros in the Code group. Next, choose ProcessHide and click Run. In the resulting input box, enter the value 1 and click OK. The macro hides the subtitle and questions prefixed with <2>.

Figure B

Add the age group subtitles.

Add a repeating header

That was so easy! The source document handled the new subtitles without any extra work on your part. Now let's suppose you want to repeat the conditional subtitle in the document's header or footer. Simple, right? Copy and paste the two subtitles to the header and then run the macro to see what happens. As you can see in Figure C, the macro doesn't evaluate content in the header. If you're using the demonstration file, close it without saving, and reopen it before continuing.

Figure C

The VBA procedure doesn't affect content in the header or footer areas.

You could update the code to specifically evaluate the header (or footer), but in this situation, there's an easier way: Use Word's {StyleRef} field. This reference field displays text automatically based on the applied style using the form:

{STYLEREF "stylename" [switch]}

You must delimit stylename as a string using the quotation marks; switch is optional. Word searches for and displays the closest text formatted using the specified style. By default, Word starts at the top of the current page. We'll add two fields; in the second, we'll apply the \l switch, which inserts the last text formatted appropriately on the current page (which will make more sense later).

The first step is to apply a style to the subtitles. Choose a style that's not currently in use, such as Subtitle. Simply select the two subtitle lines and then click Subtitle in the Styles gallery, as shown in Figure D. If you want the subtitles centered, click Center (in the Paragraph group on the Home tab) because Subtitle doesn't center the text.

Figure D

Apply the Subtitle style to the subtitle lines.

Next, open the header and insert the field as follows:

  1. Click the Insert tab.
  2. Choose Field from the Quick Parts dropdown (Figure E) in the Text group.
  3. In the resulting dialog, choose StyleRef from the Field Names list.
  4. From the Style Name list, select Subtitle (Figure F).
  5. Click OK to insert the field into the header.

Figure E

Choose Field from the Quick Parts drop-down.

Figure F

Specify the field and style.

Now here's where things get a bit tricky. Earlier, I said that this field searches for and displays the closest styled text. Consequently, the field displays only the first subtitle, if it isn't hidden. As is, if you generate a document for third through sixth grade, the document displays the appropriate subtitle, but the field in the header displays nothing. For this solution to work correctly, you must enter two fields.

So press Enter and repeat the above process, but this time, check the Search From Bottom Of Page To Top option, shown in Figure G. Because the field searches from the bottom of the current page, the field returns the first subtitle it finds, which in this case is the second subtitle from the top of the current page, if it isn't hidden.

Figure G

Add a second field that searches from the bottom of the page.

Figure H shows the resulting source document with subtitles in the body and fields in the header. When you run the macro and enter 1 in the resulting input box, the code hides the <2> lines as before. The {StyleRef} fields then display the found subtitle and nothing, accordingly.

Figure H


The macro makes the decision; the field follows suit.

Conditional sort of...

In this case, the field isn't deciding which subtitle to display. Rather, it's displaying the styled text—it only seems to be deciding between the two subtitles. This neat trick won't work if there are more than two subtitles; neither field would ever display the subtitle(s) between the first and last in the document. It isn't the only way to get the desired results, but it's easy to apply.

Send me your question about Office

I answer readers' questions when I can, but there's no guarantee. Don't send files unless requested; initial requests for help that arrive with attached files will be deleted unread. You can send screenshots of your data to help clarify your question. When contacting me, be as specific as possible. For example, "Please troubleshoot my workbook and fix what's wrong" probably won't get a response, but "Can you tell me why this formula isn't returning the expected results?" might. Please mention the app and version that you're using. I'm not reimbursed by TechRepublic for my time or expertise when helping readers, nor do I ask for a fee from readers I help. You can contact me at

Also read...

About Susan Harkins

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.

Editor's Picks

Free Newsletters, In your Inbox