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
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