I have a Word document with three levels of headlines. They are formatted properly with the Headline 1-3 styles and are also automatically numbered as e.g. 1. first level, 2.3 second level or 4.5.6 third level.

Now in my document header I used a STYLEREF field to automatically add the current first level chapter to each page, which works fine.

However, I would also like to do the same for second level headlines, but only if there is a second level headline. In other words, it should not appear on pages that have only content belonging directly to a first level headline. In that case it currently goes on searching on the following pages until it finally finds the next second level headline and shows that one, ignoring that this part hasn't started on the current page yet.

How can I display the second level headline text in the header, but only if the page contains second level content?


After another hour of painful headache over this problem, I found a solution for me:

Press Ctrl+F9 to create a field in edit mode at the current cursor location. You can switch all fields between edit and active mode using Alt+F9. Now insert this code in between the curly braces of the field (all on one line):

IF "{ STYLEREF "Headline 2" \r }" = "{ STYLEREF  "Headline 1" \w }.*" 
"{ STYLEREF  "Headline 2" \r } { STYLEREF  "Headline 2" }"

Note that you can't simply type those {} though, you must insert each pair of them with Ctrl+F9 again, so that they are recognized as nested field and not as string constant!

What does it do? The IF checks whether the chapter number of the "Headline 2" element belonging to this page is equal to the chapter number of the "Headline 1" element belonging to it (with a dot plus any number of characters appended). It effectively compares the first level chapter number.

The actual content (here chapter number and text of the corresponding "Headline 2" element) are then only displayed if the first level chapter numbers are equal. That way you prevent second level headlines from following first level chapters to appear inside the current first level chapter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.