Page numbers field on Word

S

stebain

Good day,
I have a Word report that is generated from Access and VBA.

It has a header, that I set to only show on pages 2 through n (where n is
the number of pages).
On the main page, I have a different format and create a heading (not the
Word Header) at the top of the page.
I have the Page x of n on the main page using this code:

(Sel is a vba object referring to the selection on the active document)
------------------------------
Sel.TypeParagraph
Sel.TypeText ("Report Date : ")
Sel.Fields.Add Range:=Sel.Range, Type:=wdFieldEmpty, Text:= _
"DATE ", PreserveFormatting:=True
Sel.TypeParagraph
Sel.TypeText ("Page ")
Sel.Fields.Add Sel.Range, wdFieldPage
Sel.TypeText (" of ")
Sel.Fields.Add Range:=Sel.Range, Type:=wdFieldNumPages
Sel.MoveRight unit:=wdCell

-----------------------------

For some reason, this always writes "Page 1 of 1", even when the headers on
all of the other pages show "2 of 5", "3 of 5", etc...

Any help?

Thanks,
Steve
 
S

stebain

stebain said:
Good day,
I have a Word report that is generated from Access and VBA.

It has a header, that I set to only show on pages 2 through n (where n is
the number of pages).
On the main page, I have a different format and create a heading (not the
Word Header) at the top of the page.
I have the Page x of n on the main page using this code:

(Sel is a vba object referring to the selection on the active document)
------------------------------
Sel.TypeParagraph
Sel.TypeText ("Report Date : ")
Sel.Fields.Add Range:=Sel.Range, Type:=wdFieldEmpty, Text:= _
"DATE ", PreserveFormatting:=True
Sel.TypeParagraph
Sel.TypeText ("Page ")
Sel.Fields.Add Sel.Range, wdFieldPage
Sel.TypeText (" of ")
Sel.Fields.Add Range:=Sel.Range, Type:=wdFieldNumPages
Sel.MoveRight unit:=wdCell

-----------------------------

Ick. I made something to get it to work.
Dim pageCountField as object
set pageCountField= Selection.Fields.Add Range:=Sel.Range,
Type:=wdFieldNumPages

'do a buncha junk

Selection.MoveEnd (wdStory)
'Maybe you can find a solution that works without it, but

getPageNumber Selection 'Calls the Selection.Information(wd
pageCountField.Select
Selection.Fields.Update

anyway, that did it.
 
Top