headers/footers

L

Lighthouse

How does one address headers and footers with a macro? I'm trying to
write an auto macro that examines a document section by section and if
it finds a particular marker then it needs to modify the header for the
marked section. The marked section may have 1, 2, 3, or more pages.
This is where I get confused. The header is defined for different 1st
pg, odd, and even. I don't know how to abstract these situations (is
that the right term?) in the macro. How can I be sure that I've changed
all the headers that I need to?

The documents that this will run on have a predictable structure. The
"marker" mentioned above is a particular style w/ the text "appendix."
The document structure (by section type)is either:

front matter
"normal" section
"normal" section
"normal" section
"normal" section
index

OR


front matter
"normal" section
"normal" section
"normal" section
"normal" section
"appendix" section
"appendix" section
"appendix" section
index

In the first case, nothing needs to happen. In the second case, the
macro should work its voodoo on the "appendix" sections.

When I do this manually, I go to the last appendix section and "link to
previous" for odd and even header (1st page header requires no change)
all the way forward to the *second* appendix. Then I make my header
changes in the odd/even header which is basically inserting a couple
StyleRef field codes.

Any help or insight would be appreciated. TIA
 
C

Chuck

Here's some sample code to get you started with manipulating headers. It
assumes you're familiar with for ... next loops. There are probably more
other/possibly more efficient ways of getting at headers, but hopefully the
sample code will give you ideas how to adapt the concepts to your needs...

Sub ReplaceHeaders()

Dim i As Long
Dim hdr As HeaderFooter
Dim sec As Section

With ActiveDocument
For i = 1 To .Sections.Count
'loop through each section in the
'document looking for your marker
'if found, run following code --
'make sure you IF test or SELECT CASE
'before executingg the following code so
'it doesn't run on all sections

'first make sure header isn't
'linked to previous
For Each hdr In .Sections(i).Headers
hdr.LinkToPrevious = False
Next hdr

'then make sure header in following
'section isn't linked to this section
If i <> .Sections.Count Then
For Each hdr In .Sections(i + 1).Headers
hdr.LinkToPrevious = False
Next hdr
End If

'now set your headers in the section
'that contains the marker

'if all headers the same:
For Each hdr In .Sections(i).Headers
'your code here
Next hdr

'if your headers are different:
With .Sections(i).Headers(wdHeaderFooterFirstPage)
'code for first page header
End With

With .Sections(i).Headers(wdHeaderFooterPrimary)
'code for primary (not first page) header
End With

With .Sections(i).Headers(wdHeaderFooterEvenPages)
'code for even page header
End With

'if the headers in the section are
'not all the same and you want a different
'first page header in this section:
.Sections(i).PageSetup.DifferentFirstPageHeaderFooter = True

Next i

End With

End Sub

HTH
Chuck
 
L

Lighthouse

Wow! Chuck that looks like it fits my bill exactly. I'll give it a shot
today or tomorrow. THanks a bunch.
 

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