Setting up running headers in a document by using STYLEREF FIELDS

A

andreas

Hello,

I recently wrote a macro code that is setting up running headers (odd
and even) in long documents with several sections (all headers and
footers are not linked to previous).

The odd page headers show the text from the numbered heading 1 levels
and I achieved this by telling Word:

Set rng = sect.Headers(wdHeaderFooterPrimary).Range
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:= _
"STYLEREF ""Heading 1"" \n "
rng.Collapse wdCollapseEnd
rng.Text = " "
rng.Collapse wdCollapseEnd
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:= _
" STYLEREF ""Heading 1"" "


Now the even page headers show the text from the numbered heading 2
levels:

Set rng = sect.Headers(wdHeaderFooterEvenPages).Range
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:= _
"STYLEREF ""Heading 2"" \n "
rng.Collapse wdCollapseEnd
rng.Text = " "
rng.Collapse wdCollapseEnd
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:= _
" STYLEREF ""Heading 2"" "

The trouble is that there are a couple of sections that haven't!! got a
heading level 2 (just a heading level 1). In these sections, the above
macro code causes Word to insert the text from heading level 2 on even
page headers from the previous section.

Hence my question: How can I tell Word that whenever a section hasn't
got a heading level 2, all headers (odd and even) should show only the
text from the heading level 1

Help is appreciated.

thank you in advance.
 
P

Peter Jamieson

If your document are "fire and forget", i.e. the user can do anything with
them after they are created and you can't get them to run macros, then I do
not think you can do anything about this.

If you can postprocess the document that the user creates, you should be
able to detect the presence or absence of a heading style in each section
and modify your header/footer accordingly using VBA.

If you can get the users to collaborate, or if, for example, you are able to
provide a macro that inserts paragraphs with styles Heading 1 and Heading 2,
then you could do something along the following lines:
a. whenever you insert a Heading 2 style paragraph, insert a { SECTION }
field either at the beginning or the end of the text, formatted so that its
result is invisible. The easiest approach is to add a { SECTION } field at
the end, formatted as white text so that it cannot be seen.
b. use the following nested field in your header:

{ IF "{ STYLEREF "Heading 2" }" = "*{ SECTION }" "{ STYLEREF "Heading 2" }"
"whatever text you want if there is no heading 2 style in the section" }

This has a chance of working only if
a. the { SECTION } field is in all the necessary Heading 2 style paragraphs
b. the heading text is not too long for the comparison to fail (I think it
may do so if the text is longer than around 127 or 255 characters, and
perhaps in other circumstances)

There may be other ways to do this so that the user can't accidentally
undermine the approach, but that's the one I can think of right now.

Peter Jamieson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top