Use STYLEREF, PAGEREF, and IF Fields in Word Headers
Question details
Create Word headers that display a Heading 2 when appropriate and fall back to Heading 1, with separate odd and even headers if needed.

- Product
- Microsoft Word
- Device & OS
- not provided
- Scenario
- A document uses heading styles and needs conditional header text, potentially with page-reference logic.
- Observed behavior
- STYLEREF has no direct page-reference switch, and Word has no documented TRIM calculation field. Hidden heading markers can support the heading logic but may not produce the desired page-number behavior.
Apply Heading 1 and Heading 2 styles consistently, and decide which pages should use each header. In Word, use Home > ¶ to show formatting marks and hidden text while placing markers.
Use hidden Heading 2 markers and an IF field
Use this workaround to select Heading 2 or fall back to Heading 1 without a TRIM field or a STYLEREF page-reference switch.
STYLEREF searches the current page for the specified style and can fall back to an earlier occurrence. Hidden Heading 2 paragraphs provide a recognizable marker for the IF test, but their placement affects the result.
On the first page, add a paragraph containing NO_SUBHEADING and apply Home > Styles > Heading 2. Select the marker text and paragraph mark, then open the Font dialog and select Hidden.
Add another hidden NO_SUBHEADING paragraph in Heading 2 style after each actual Heading 2. Check its position against page breaks so that STYLEREF finds the intended heading or marker.
Double-click the header area. If you need separate designs, enable Different Odd & Even Pages on the Header & Footer tab and edit each header separately.
Insert this field structure in the header: { IF "{ STYLEREF "Heading 2" }" = "NO_SUBHEADING" "{ STYLEREF "Heading 1" }" "{ STYLEREF "Heading 2" }" }. Use Ctrl+F9 for each pair of field braces rather than typing the braces, and enter the remaining text inside them.
Select the header field and press F9, then use File > Print to preview pages with and without Heading 2. Adjust marker placement where the displayed heading is not the one you want.

Compare a bookmarked heading’s page with PAGEREF
Use a PAGEREF comparison when the condition concerns a specific bookmarked heading rather than an automatically identified current heading.
Edit Word documents with WPS Writer
WPS Office is a free, lightweight alternative with a familiar interface for opening and editing Word documents. Move existing files into WPS Writer easily, but verify complex conditional fields and headers before relying on them.

Frequently Asked Questions
Can STYLEREF return the page number of the heading it finds?
STYLEREF has no direct page-reference switch. PAGEREF can return the page of a named bookmark, but it does not automatically refer to the heading selected by STYLEREF.
How do I insert nested Word field braces?
In the header, press Ctrl+F9 for each pair of field braces. Type the field instructions inside the braces, then select the completed field and press F9 to update it.
Why does my header show the hidden marker instead of a heading?
STYLEREF may find the marker first on the page. Show formatting marks with Home > ¶, inspect the marker and heading positions, and adjust the placement before updating the header field.
How do I make odd and even headers different?
Double-click a header and enable Different Odd & Even Pages on the Header & Footer tab. Edit the odd-page and even-page headers separately.
Is there a TRIM calculation field in Word?
Word does not provide a documented TRIM calculation field. For this header scenario, use an exact marker comparison in an IF field instead of relying on TRIM.




