Problems with PageNumbers.Add on first page of a section

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
 
W

Word Heretic

G'day "Lee Robinson" <[email protected]>,

use range.fields.add range,...

and add a Page field.



Steve Hudson - Word Heretic
Want a hyperlinked index? S/W R&D? See WordHeretic.com

steve from wordheretic.com (Email replies require payment)


Lee Robinson reckoned:
 
W

Word Heretic

G'day "Lee Robinson" <[email protected]>,

Splice it into your existing code

Dim Insert as Range

....

Set Insert= .Footers.Item(wdHeaderFooterFirstPage).range
Insert.collapse '(to stick it at the start of the footer)
Activedocument.fields.add Insert,wdFieldPage



Steve Hudson - Word Heretic
Want a hyperlinked index? S/W R&D? See WordHeretic.com

steve from wordheretic.com (Email replies require payment)


Lee Robinson reckoned:
 

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