Making sections with their own header and footer

O

Ogier

Hi out there!

I am trying to understand the interaction between sections and
headers/footers in Word. As I see it each section can have its own header and
its own footer, but I have had some trouble inserting headers and footers
properly. With the macros I have written (see below) section changes and
dummy texts are inserted properly, but the headers and footers get jumbled
together. For instance the footer appearing on the first page is

Footer belonging to section 3.Footer belonging to section 2.Footer belonging
to section 1.

I have tried to change the order of the statements in the subroutines
InsertNewHeader and InsertNewFooter, but with no success.

Here then are my questions:
1. If a page contains a section break separating section A from section B,
will the header displayed on that page be that of section A and the footer
that of section B? Or does the section with which the page begins (A)
determine both?
2. How must I change my code below so that each section has its own header
and footer?
3. For aesthetic reasons I don’t want a header on my first page, but the
footer should be displayed. Is that possible? I know the setting “Special
first pageâ€, but as far as I can tell this affects both header and footer.
4. Can the deletion of previous text
be carried out more elegantly than done in subroutine NewTestDocument?

Best wishes
Holger

-----

Sub InsertNewHeader(Number As String)
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.TypeText Text:="Header belonging to section " + Number + "."
Selection.HeaderFooter.LinkToPrevious = Not Selection.HeaderFooter. _
LinkToPrevious
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub

Sub InsertNewFooter(Number As String)
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
Selection.TypeText Text:="Footer belonging to section " + Number + "."
Selection.HeaderFooter.LinkToPrevious = Not Selection.HeaderFooter. _
LinkToPrevious
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub

Sub InsertDummyText(Number As String)
Dim i As Integer, j As Integer, iMax As Integer
If Number = "1" Then
iMax = 1
Else
iMax = 4
End If
For i = 1 To iMax
For j = 1 To 45
Selection.TypeText Text:="Just some dummy text in section " +
Number + ". "
Next j
Selection.TypeParagraph
Next i
Selection.TypeParagraph
End Sub

Sub InsertNewSection(Number As String)
Selection.InsertBreak Type:=wdSectionBreakContinuous
InsertNewHeader Number
InsertNewFooter Number
Selection.TypeText Text:="Here begins section " + Number
Selection.TypeParagraph
InsertDummyText Number
End Sub

Sub NewTestDocument()
Dim Section As Integer
' Clear document for prevoius test results
Selection.WholeStory
Selection.Delete Unit:=wdCharacter, Count:=1
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.WholeStory
Selection.Delete Unit:=wdCharacter, Count:=1
WordBasic.GoToFooter
Selection.WholeStory
Selection.Delete Unit:=wdCharacter, Count:=1
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
' Write a heading text
Selection.TypeText Text:="This is the title of this document"
Selection.TypeParagraph
Selection.TypeText Text:="There should be no header on this page, but
the footer should be there!"
Selection.TypeParagraph
InsertDummyText "1"
InsertNewFooter "1"
' Now write some sections
For Section = 2 To 5
InsertNewSection (CStr(Section))
Next Section
End Sub
 
P

Pesach Shelnitz

Hi Holger,

First, I'll try to answer your questions.
1. If you have a section break on a page, both the header and the footer
will be for the section at the beginning of the page.
2. I hope that the changes that I made to your code will do this.
3. It is possible. The "Different First Page" option affects both the header
and the footer, but if you don't want to see a header on the first page, just
don't put anything into it.
4. Your code for deleting the body text is fine. However, for the headers
and footers, I changed your code to cycle through the sections first and
delete all the headers and footers through their Range objects without all
the cursor movements.

Try this version of your code with my changes.

Sub NewTestDocument()
Dim SecNum As Integer
Dim sect As Section
Dim hdft As HeaderFooter

' Clear document for prevoius test results
With ActiveDocument
For Each sect In .Sections
For Each hdft In sect.Headers
hdft.Range.Delete
Next
For Each hdft In sect.Footers
hdft.Range.Delete
Next
Next
End With
Selection.WholeStory
Selection.Delete

' Write a heading text
Selection.TypeText Text:="This is the title of this document"
Selection.TypeParagraph
Selection.TypeText Text:="There should be no header on this page, but
the footer should be there!"
Selection.TypeParagraph

InsertDummyText "1"
InsertNewFooter "1"
' Now write some sections
For SecNum = 2 To 5
InsertNewSection (CStr(SecNum))
Next SecNum

End Sub

Sub InsertNewHeader(Number As String)
With ActiveDocument.Sections(Number)
If Number <> 1 Then
With .Headers(wdHeaderFooterPrimary)
.LinkToPrevious = False
.Range.Text = "Header belonging to section " + Number + "."
End With
End If
End With
End Sub

Sub InsertNewFooter(Number As String)
With ActiveDocument.Sections(Number)
If Number = 1 Then
With .Footers(wdHeaderFooterFirstPage)
.Range.Text = "Footer belonging to section " + Number + "."
End With
End If
With .Footers(wdHeaderFooterPrimary)
.LinkToPrevious = False
.Range.Text = "Footer belonging to section " + Number + "."
End With
End With
End Sub

Sub InsertDummyText(Number As String)
Dim i As Integer, j As Integer, iMax As Integer
If Number = "1" Then
iMax = 1
Else
iMax = 4
End If
For i = 1 To iMax
For j = 1 To 45
Selection.TypeText Text:="Just some dummy text in section " +
Number + ". "
Next j
Selection.TypeParagraph
Next i
Selection.TypeParagraph
End Sub

Sub InsertNewSection(Number As String)
Selection.InsertBreak Type:=wdSectionBreakContinuous
InsertNewHeader Number
InsertNewFooter Number
Selection.TypeText Text:="Here begins section " + Number
Selection.TypeParagraph
InsertDummyText Number
End Sub

Let us (the newsgroup) know if you have any more questions.
 
O

Ogier

Hi Pesach

Thanks a lot for your answers to my questions and the your improved code!
Now it works precisely as I wished it should!
I'll study your changes to improve my knowledge of macro programming.

Best wishes
Holger
 
G

Greg Maxey

The following code should achieve what I think you tried to explain:

Sub NewTestDocument()
Dim Section As Long
Dim i As Long
Dim oStoryRng As Range
'Clear existing text
i = ActiveDocument.Sections(1).Headers(1).Range.StoryType
For Each oStoryRng In ActiveDocument.StoryRanges
'Iterate through all linked stories
Do
oStoryRng.Delete
Set oStoryRng = oStoryRng.NextStoryRange
Loop Until oStoryRng Is Nothing
Next
ActiveDocument.Range.Text = "This is the title of this document" & vbCr +
vbCr & _
"There should be no header on this page, but the footer should be there!"
& vbCr + vbCr
ActiveDocument.Sections(1).PageSetup.DifferentFirstPageHeaderFooter = True
InsertDummyText "1"
InsertNewHeader "1"
InsertNewFooter "1"
'Now write some sections
For Section = 2 To 5
InsertNewSection (CStr(Section))
Next Section
End Sub
Sub InsertDummyText(Number As String)
Dim i As Long, j As Long, iMax As Long
If Number = "1" Then
iMax = 1
Else
iMax = 4
End If
For i = 1 To iMax
For j = 1 To 45
ActiveDocument.Range.InsertAfter "Just some dummy text in section" &
Number & "." & vbCr
Next j
ActiveDocument.Range.InsertAfter vbCr
Next i
End Sub
Sub InsertNewSection(Number As String)
Dim oRng As Range
Set oRng = ActiveDocument.Range
With oRng
.Collapse Direction:=wdCollapseEnd
.InsertBreak Type:=wdSectionBreakNextPage
End With
InsertNewHeader Number
InsertNewFooter Number
With oRng
.Collapse Direction:=wdCollapseEnd
.InsertAfter "Here begins section " & Number & "." & vbCr + vbCr
End With
InsertDummyText Number
End Sub
Sub InsertNewHeader(Number As String)
Dim oHeader As HeaderFooter
Set oHeader = ActiveDocument.Sections(Number).Headers(wdHeaderFooterPrimary)
With oHeader
.LinkToPrevious = False
.Range.Text = "Header belonging to section " & Number & "."
End With
End Sub
Sub InsertNewFooter(Number As String)
Dim oFooter As HeaderFooter
'Handlee the section 1 different first page
If Number = "1" Then
Set oFooter =
ActiveDocument.Sections(Number).Footers(wdHeaderFooterFirstPage)
With oFooter
.LinkToPrevious = False
.Range.Text = "Footer belonging to section " & Number & "."
End With
End If
'Handler all remaining footers
Set oFooter = ActiveDocument.Sections(Number).Footers(wdHeaderFooterPrimary)
With oFooter
.LinkToPrevious = False
.Range.Text = "Footer belonging to section " & Number & "."
End With
End Sub

I don't claim to be the authority on sections, breaks, or headers and
footers, but I can try to explain a few things.

1. Each section (i.e., sections that start a new page not "continous"
sections) of a document has 3 types of header and 3 types of footers. You
may not always see them, but they are they are there. The is a first page
header and footer, an even page header and footer, and finally a primary (or
odd) page header and footer. By default a blank new word document (based on
Normal.dot) starts out displaying only primary headers and footers. You can
display the first page and even page headers and footers by set page setup
options for different first page and different odd and even.

2. Again by default the headers/footers in section 2 are linked to those in
section 1. Those is section 3 are linked to section 2. It is sort of like
the old player piano sheets that continue the same thing over and over. So
by extension the headers and footers in section 1 are linked to those in the
last section. You change one they all change.

3. You can break this link with the "LinkToPrevious" attribute, but
remember you must break the link before changing the text.
 

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