L
Lee Robinson
I am trying to write a routine to go through my document, find the sections
containing a Heading 1 and fix the headers and footers so that the page
number shows in the footer on the first page but in the header on all of the
other pages. However, when I issue this command:
s.Footers.Item(wdHeaderFooterFirstPage).PageNumbers.Add
wdAlignPageNumberCenter, True
or even this one
s.Footers.Item(wdHeaderFooterFirstPage).PageNumbers.Add
I get an error saying subscript out of range. However, when I check, a
pagenumber has been created in the EvenPages footer and in the primary
footer, even though I was adding to the FirstPage footer. On top of that,
the
s.PageSetup.DifferentFirstPageHeaderFooter
has been set to false even though I set it to true just before the "Add".
Any idea what I am running into here? I can set up the pagenumbers the way
I want in the document using the mouse and the menus but I would like to be
able to automate the process using VBA. Here is the code that causes the
problem.
Dim hdr As HeaderFooter
Dim s As Section
For Each s In ActiveDocument.Sections
' Here I clear out the page numbers
For Each hdr In s.Headers
Do
For Each pgn In hdr.PageNumbers
pgn.Delete
Next
Loop Until hdr.PageNumbers.Count = 0
Next
For Each hdr In s.Footers
Do
For Each pgn In hdr.PageNumbers
pgn.Delete
Next
Loop Until hdr.PageNumbers.Count = 0
Next
' Here I unlink the headers and footers
For Each hdr In s.Headers
hdr.LinkToPrevious = False
Next
For Each hdr In s.Footers
hdr.LinkToPrevious = False
Next
" This shows me that I don't have any stray page numbers
Debug.Print s.Footers.Item(wdHeaderFooterEvenPages).PageNumbers.Count
Debug.Print s.Footers.Item(wdHeaderFooterFirstPage).PageNumbers.Count
Debug.Print s.Footers.Item(wdHeaderFooterPrimary).PageNumbers.Count
If I issue this command, then a pagenumber gets created in EvenPages and
Primary and the DifferentFirstPage flag gets turned off (False):
s.Footers.Item(wdHeaderFooterFirstPage).PageNumbers.ShowFirstPageNumber =
True
If I don't issue the command shown above but issue this one:
s.Footers.Item(wdHeaderFooterFirstPage).PageNumbers.Add
wdAlignPageNumberCenter, True
Then the same thing happens and the error message shows.
Any idea what is going on? Am I doing this in the wrong order or to the
wrong objects?
Lee Robinson
containing a Heading 1 and fix the headers and footers so that the page
number shows in the footer on the first page but in the header on all of the
other pages. However, when I issue this command:
s.Footers.Item(wdHeaderFooterFirstPage).PageNumbers.Add
wdAlignPageNumberCenter, True
or even this one
s.Footers.Item(wdHeaderFooterFirstPage).PageNumbers.Add
I get an error saying subscript out of range. However, when I check, a
pagenumber has been created in the EvenPages footer and in the primary
footer, even though I was adding to the FirstPage footer. On top of that,
the
s.PageSetup.DifferentFirstPageHeaderFooter
has been set to false even though I set it to true just before the "Add".
Any idea what I am running into here? I can set up the pagenumbers the way
I want in the document using the mouse and the menus but I would like to be
able to automate the process using VBA. Here is the code that causes the
problem.
Dim hdr As HeaderFooter
Dim s As Section
For Each s In ActiveDocument.Sections
' Here I clear out the page numbers
For Each hdr In s.Headers
Do
For Each pgn In hdr.PageNumbers
pgn.Delete
Next
Loop Until hdr.PageNumbers.Count = 0
Next
For Each hdr In s.Footers
Do
For Each pgn In hdr.PageNumbers
pgn.Delete
Next
Loop Until hdr.PageNumbers.Count = 0
Next
' Here I unlink the headers and footers
For Each hdr In s.Headers
hdr.LinkToPrevious = False
Next
For Each hdr In s.Footers
hdr.LinkToPrevious = False
Next
" This shows me that I don't have any stray page numbers
Debug.Print s.Footers.Item(wdHeaderFooterEvenPages).PageNumbers.Count
Debug.Print s.Footers.Item(wdHeaderFooterFirstPage).PageNumbers.Count
Debug.Print s.Footers.Item(wdHeaderFooterPrimary).PageNumbers.Count
If I issue this command, then a pagenumber gets created in EvenPages and
Primary and the DifferentFirstPage flag gets turned off (False):
s.Footers.Item(wdHeaderFooterFirstPage).PageNumbers.ShowFirstPageNumber =
True
If I don't issue the command shown above but issue this one:
s.Footers.Item(wdHeaderFooterFirstPage).PageNumbers.Add
wdAlignPageNumberCenter, True
Then the same thing happens and the error message shows.
Any idea what is going on? Am I doing this in the wrong order or to the
wrong objects?
Lee Robinson