How to get page number with VBA in Word ?

D

Daniel

It could be simple, but I can not find how to do it:

I need a code to get the page number of some range object, let me say, I
have a paragraph, and I want to know his location (page number) when printed.

Thanks, yours, Daniel
from Brazil
 
J

Jean-Guy Marcil

Daniel was telling us:
Daniel nous racontait que :
It could be simple, but I can not find how to do it:

I need a code to get the page number of some range object, let me
say, I have a paragraph, and I want to know his location (page
number) when printed.

Did you see the post named : "How to get the current page number?" from 3
days ago?

In VBA you would use the Information property:

MsgBox Selection.Information(wdActiveEndPageNumber)
MsgBox Selection.Information(wdActiveEndAdjustedPageNumber)

where the first one gives you the absolute page count in the document and
the second the page number as defined in the Page Number formatting dialog
(Insert menu > Page Numbers...)


--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
M

m g

I dont success know page number to some paragraph
any idia?




Danie wrote:

How to get page number with VBA in Word ?
12-Mar-07

It could be simple, but I can not find how to do it

I need a code to get the page number of some range object, let me say, I
have a paragraph, and I want to know his location (page number) when printed

Thanks, yours, Danie
from Brazil

EggHeadCafe - Software Developer Portal of Choice
Excel Conditional Hiding Without VBA
http://www.eggheadcafe.com/tutorial...15-b0106033e45c/excel-conditional-hiding.aspx
 
J

Jay Freedman

Let's assume you have a Range object that points to the paragraph or
something inside the paragraph (a bookmark, a field, a table, etc.), and
assume that Range object is named MyRange. Then the *absolute* page number
(simply counting pages from the start of the document, regardless of any
page numbering restarts in intervening sections) of the end of the paragraph
is

MyRange.Information(wdActiveEndPageNumber)

while the *adjusted* page number (taking into account page numbering
restarts, what would show if you put a {PAGE} field there) of the end of the
paragraph is

MyRange.Information(wdActiveEndAdjustedPageNumber)

There's an additional wrinkle if you're dealing with a selection made by the
user; if the selection was made by starting at the end of the paragraph and
selecting toward the beginning, and if the selection crosses a page
boundary, then the page number you get from the code above will be the page
that contains the beginning of the range (which is the "active" end).

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
D

Dale Fye

Jay,

I was just about to ask an almost identical question, except focusing on a
document that I am buiding from scratch. Ended up using :

wdDoc.ActiveDocument.Selection.Information(wdActiveEndPageNumber) to get
what I needed.
 

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