PDA

View Full Version : Determining Page Number



MWE
03-09-2006, 11:17 AM
This question seems pretty basic, but is turning out to be rather convoluted. Given a selection, how to I determine the page number for the selection? or if the selection runs several pages, how do I determine the page number for the start of the selection?

EDIT: I have a Range, not a Selection. I could select the range, but it is easier to not do that. I wish to determine the page number for the start of the range and the page number for the end of the range.

mdmackillop
03-09-2006, 02:10 PM
Hi MWE
Check out the Information property in Help

MsgBox "The selection is on page " & _
Selection.Information(wdActiveEndPageNumber) & " of page " _
& Selection.Information(wdNumberOfPagesInDocument)

Regards
MD

Jacob Hilderbrand
03-09-2006, 02:10 PM
Something like this should get you started.

Dim PageStart As Long
Dim PageEnd As Long

PageEnd = Selection.Information(wdActiveEndAdjustedPageNumber)
Selection.MoveLeft Unit:=wdCharacter, Count:=1
PageStart = Selection.Information(wdActiveEndAdjustedPageNumber)

MWE
03-09-2006, 02:29 PM
Hi MWE
Check out the Information property in Help

MsgBox "The selection is on page " & _
Selection.Information(wdActiveEndPageNumber) & " of page " _
& Selection.Information(wdNumberOfPagesInDocument)

Regards
MD thanks for the reply. This is helpful but generates incorrect numbers. Everything seems to be off by 1 or 2 page numbers. I also tried EndAdjustedPageNumber and got similar, incorrect, results. It also does not help me know the start AND end page number of the range (not Selection but Range; Selection has properties that are not supported in Range). See original, now modified, post.

MWE
03-09-2006, 02:31 PM
Something like this should get you started.

Dim PageStart As Long
Dim PageEnd As Long

PageEnd = Selection.Information(wdActiveEndAdjustedPageNumber)
Selection.MoveLeft Unit:=wdCharacter, Count:=1
PageStart = Selection.Information(wdActiveEndAdjustedPageNumber)
Thanks. Unfortunately this method generates incorrect page numbers and the MoveLeft does not work with a Range. See original, now modified, post.

mdmackillop
03-09-2006, 02:45 PM
Sub Pages()
Dim MyRange1 As Range
Dim MyRange2 As Range
Set MyRange1 = ActiveDocument.Range(Start:=0, End:=0)
MyRange1.SetRange Start:=Selection.Start, End:=Selection.Start
Set MyRange2 = ActiveDocument.Range(Start:=0, End:=0)
MyRange2.SetRange Start:=Selection.End, End:=Selection.End
MsgBox "The selection is on pages " & _
MyRange1.Information(wdActiveEndPageNumber) & " to " & _
MyRange2.Information(wdActiveEndPageNumber) & _
" of " & Selection.Information(wdNumberOfPagesInDocument)
End Sub

MWE
03-09-2006, 05:01 PM
Sub Pages()
Dim MyRange1 As Range
Dim MyRange2 As Range
Set MyRange1 = ActiveDocument.Range(Start:=0, End:=0)
MyRange1.SetRange Start:=Selection.Start, End:=Selection.Start
Set MyRange2 = ActiveDocument.Range(Start:=0, End:=0)
MyRange2.SetRange Start:=Selection.End, End:=Selection.End
MsgBox "The selection is on pages " & _
MyRange1.Information(wdActiveEndPageNumber) & " to " & _
MyRange2.Information(wdActiveEndPageNumber) & _
" of " & Selection.Information(wdNumberOfPagesInDocument)
End Sub

Thanks. The method you used is pretty nice. Unfortunately, it still generates wrong page numbers some of the time.

In my application, I am building a table of data on specific paragraphs found in the document (based on certain key words). When a keyword match is found for a paragraph, the target range "captured" in a range variable called "ParaRange". A bookmark is generated using ParaRange as the new bookmark's range and a hyperlink is generated from the table entry to the bookmark. The paragraph's text is properly captured in ParaRange, the ParaRange.Range text is used to fill a table cell and that is correct; and the hyperlink is correct. So I know that ParaRange is the "right range".

I used your code substituting ParaRange for Selection. The page number results are quite frustrating: :banghead:

The final table has 39 rows. The page numbers for the first 4 rows are off by 2 (VBA # is 2 smaller than actual). The page numbers for the next 15 or 20 rows are off by 1 (VBA # is 1 smaller than actual). The page numbers for the last 10 or so rows are correct. The method does capture those cases where the range covers two page (in those few cases, both start and end were wrong but end was = start + 1)

It seems like there is a scaling or transformation issue and the "internally known" page numbers (what VBA fetches) are different from the actual or printed page numbers. There is only one section in the document. The screen view using PrintLayout (Word2k) and actual printer results are the same. I also tried using EndAdjustPageNumber, but the results were the same.

MWE
03-09-2006, 05:02 PM
Sub Pages()
Dim MyRange1 As Range
Dim MyRange2 As Range
Set MyRange1 = ActiveDocument.Range(Start:=0, End:=0)
MyRange1.SetRange Start:=Selection.Start, End:=Selection.Start
Set MyRange2 = ActiveDocument.Range(Start:=0, End:=0)
MyRange2.SetRange Start:=Selection.End, End:=Selection.End
MsgBox "The selection is on pages " & _
MyRange1.Information(wdActiveEndPageNumber) & " to " & _
MyRange2.Information(wdActiveEndPageNumber) & _
" of " & Selection.Information(wdNumberOfPagesInDocument)
End Sub
Thanks. The method you used is pretty nice. Unfortunately, it still generates wrong page numbers some of the time.

In my application, I am building a table of data on specific paragraphs found in the document (based on certain key words). When a keyword match is found for a paragraph, the target range is "captured" in a range variable called "ParaRange". A bookmark is generated using ParaRange as the new bookmark's range and a hyperlink is generated from the table entry to the bookmark. The paragraph's text is properly captured in ParaRange, the ParaRange.Range text is used to fill a table cell and that is correct; and the hyperlink is correct. So I know that ParaRange is the "right range".

I used your code substituting ParaRange for Selection. The page number results are quite frustrating: :banghead:

The final table has 39 rows. The page numbers for the first 4 rows are off by 2 (VBA # is 2 smaller than actual). The page numbers for the next 15 or 20 rows are off by 1 (VBA # is 1 smaller than actual). The page numbers for the last 10 or so rows are correct. The method does capture those cases where the range covers two page (in those few cases, both start and end were wrong but end was = start + 1)

It seems like there is a scaling or transformation issue and the "internally known" page numbers (what VBA fetches) are different from the actual or printed page numbers. There is only one section in the document. The screen view using PrintLayout (Word2k) and actual printer results are the same. I also tried using EndAdjustPageNumber, but the results were the same.

mdmackillop
03-09-2006, 05:14 PM
I didn't test it with tables, but with a simple 10 page document which gave the correct results with simple text selection. I'll try sticking a table into it and see what happens!

Just looking at the Help file. Does wdActiveEndAdjustedPageNumber return a different value?

MWE
03-09-2006, 06:09 PM
I didn't test it with tables, but with a simple 10 page document which gave the correct results with simple text selection. I'll try sticking a table into it and see what happens!

Just looking at the Help file. Does wdActiveEndAdjustedPageNumber return a different value?
Don't bother checking with a table; it just dawned on me what is wrong. The table is being built at the top of the document. Each row added moves everything down a bit and, thus, the original page for a given paragraph is not the same as the final page. STUPID MWE!! :banghead: This is easy to fix.

Thanks for your help (again) :thumb:thumb

fumei
03-09-2006, 06:13 PM
Gentlemen. Page numbers are slippery things in Word.

MWE
"internally known" page numbers (what VBA fetches) are different from the actual or printed page - while it probably is not relevant for you, as you are likely using the same machine for yourself, please note that you would also get different numbers on different machines - even with the same document. This is because all page numbers are calculated dynamically by Word. And it does NOT store them. In other words, there ARE no "internally known" page numbers.
In my application, I am building a table of data on specific paragraphs found in the document (based on certain key words). When a keyword match is found for a paragraph, the target range is "captured" in a range variable called "ParaRange". A bookmark is generated using ParaRange as the new bookmark's range and a hyperlink is generated from the table entry to the bookmark. The paragraph's text is properly captured in ParaRange, the ParaRange.Range text is used to fill a table cell and that is correct; and the hyperlink is correct. So I know that ParaRange is the "right range". I am trying to follow this. Your statement states that you are filling a table with with the text of an identified paragraph.

Surely not.

If the hyperlink works correctly, why do you care if you get a page number?

It is possible to get accurate page numbers calculated, but you sometimes have to coax it out.

MWE
03-09-2006, 07:30 PM
...I am trying to follow this. Your statement states that you are filling a table with with the text of an identified paragraph.

Surely not.

If the hyperlink works correctly, why do you care if you get a page number?
thanks for the comments and background ...

Yes, I am filling the table with selected text from identified paragraphs, and hyperlinks to the original paragraphs, and page numbers. So, why am I doing this?

The document is 40 pages on its way to probably 75 pages. Before it can be approved, every TBD in the document must be placed in a separate table with an owner (to fix it). There are presently 40 paragraphs with one or more TBDs in it. There may be 100 before the first version of the document is approved. The table provides a reasonable way to generate an action item list.

The hyperlinks provide an easy way for the (electronic) reader to jump to the target paragraph and read background to better understand what the TBD really means.

The page numbers provide a way for the hardcopy reader to quickly find the target paragraph and ...

You are correct that different machines will generate potentially different page numbers. But it actually worse than that. Sending output to different printers from the same machine can generate different page numbers. We are all familiar with the Word process that renumbers tables before a job is printed -- same issues with my table; and I will probably approach the final solution the same way.

Thanks again.

fumei
03-09-2006, 08:58 PM
RE: different printers, different page numbers. That is because Word uses the printer driver to determine the page number. That is how it calculates them.
Yes, I am filling the table with selected text from identified paragraphsFair enough. How are you determining selected text?

It is crucial to get the accurate page numbers - and I am not sure you can maintain that since the document seems to be fluid and quite dynamic - is to get the page numbers after the table is built. Loop through the table and jump - using the Selection (and you would probablyt want to turn ScreenUpdating to False) - to the page and grab the page number, and then jump back to the table to put the page number in as text.

MWE
03-10-2006, 12:43 PM
RE: different printers, different page numbers. That is because Word uses the printer driver to determine the page number. That is how it calculates them.Fair enough. How are you determining selected text?

It is crucial to get the accurate page numbers - and I am not sure you can maintain that since the document seems to be fluid and quite dynamic - is to get the page numbers after the table is built. Loop through the table and jump - using the Selection (and you would probablyt want to turn ScreenUpdating to False) - to the page and grab the page number, and then jump back to the table to put the page number in as text.
Selected text is presently determined by indexing through paragraphs and looking for specific keywords. That is a slow and cumbersome way to do this, but initially there was a reason. When I have some time to optimize the code, I will switch to Find

Once I realized what the probem with page numbers was, it was easy to get the right ones. I do not bother to search twice, I simply keep a list of bookmarks (for the hyperlinks) and then go back and get the start/end of each bookmark. Works fine.

The document is pretty dynamic, but will be frozen each time it is updated and approved. The only lingering problem (other than some performance tweaks) is how to manage page numbers on multiple systems/printers. I am using VBA to build the document and to manage messy things during the development process. But once a version is approved, the VBA can not remain (regulated environment and ver/val of code is quite involved). This page number thing is the only issue I have encountered where the resulting document can not stand alone (without VBA). Maybe the simpliest thing to do is not use page numbers in the table at all.

mdmackillop
03-10-2006, 01:08 PM
With no VBA permitted, the only thing I can suggest is that you inset manual page breaks with pages short enough that they would not be topped and tailed by any "normal" printer settings. Maybe not the best appearance, but it seems we can't have everything!

fumei
03-10-2006, 07:07 PM
Are you saying you will not be able to have VBA in the final document? Actually, since you ARE talking about TBD, then would not the final document haveno TBD? In which case, it seems to me that there would be no need for the table.

In any case, if you CAN have VBA during the development stage, you could have code that runs with printing. When the document is printed, then and only then, does it first figure current page numbers.

TonyJollans
03-11-2006, 06:21 PM
I haven't followed the entire discussion here but what I think you want is a PAGEREF Field which needs no VBA to insert or operate.

MWE
03-11-2006, 06:54 PM
I haven't followed the entire discussion here but what I think you want is a PAGEREF Field which needs no VBA to insert or operate.
Pretty neat :thumb

After reading through the Help stuff on this field code, this should work quite well. It even provides an integrated way to generate the hyperlink.

Thanks

MWE
03-12-2006, 10:30 PM
I haven't followed the entire discussion here but what I think you want is a PAGEREF Field which needs no VBA to insert or operate.
I have looked at this and there is a problem using field codes (at least with my version of Word2000). Field codes are not automatically updated when the document is saved or opened. I poked around the Help utility and even it is misleading. It suggests that you do a select all and then hit F9. That works for the document body field codes, but not for the headers and footers. The latter is not necessarily a problem for my need, but it is a pain generally. There are some quirky things about the way Word handles field codes and printing. For example, if you have a field code for DateSaved and save the document, that field code will NOT update. If you then print the document, the printed version will have the correct data/time for DateSaved. But if you close and reopen the document, the DateSaved field code is back where it started, i.e., its date/time before it was last saved.

I am not sure why I did not think of this last night ... I encountered this problem a month or so ago and wrote a little utility that updates all field codes (body, headers and footers)

TonyJollans
03-13-2006, 01:16 AM
Field codes in headers and footers are treated differently from those in the document body. I think you've answered your own question but if not come back :)

If you set the option to update fields before printing it can cause confusion sometimes, I agree.