Computer geek, and self-appointed know-it-all, Westley Annis answers all those hard 

questions about anything related to computers and technology, as well as business and 

political questions.

What page am I on?

I've got a macro that searches through Word documents looking for broken bookmarks and TOC entries and generates a report so they can be fixed. The problem is I can find the broken elements, but I don't know what page they're on so I can manually find them and fix them. How do I find out what page number I'm on inside of a macro?

Unless you live and breathe the Microsoft Word object model, it can be difficult at times to figure out where to find the values you are looking for. Word is composed of a lot of different objects, each of which have different properties, although those properties may be repeated among some objects.

In this case you are looking for page numbers. These can be found using the Information property. You supply a Word constant that specifies the type of information you want returned. In this case, you have two options that jump right out: wdActiveEndPageNumber and wdActiveEndAdjustedPageNumber.

The first option will return the actual page number, ignoring any manual adjustments to the page numbers. The first page is page one even if it prints out as page 16. The latter option will adjust the page count based on your changes. So if you've specified that the first page will print as page 16, that is the number that is returned. The second page would be 17 and so on.

Exactly how do you use the Information property?

To get the page number for the current selection:

intCurrentPage = Selection.Information(wdActiveEndPageNumber)

To get the page number for a Bookmark:

intCurrentPage = Activedocument.Bookmarks(1).Range.Information(wdActiveEndPageNumber)

There is so much more available from the Information property. You can easily tell if the current selection is inside of a table, the column and row when the selection is inside a table, header and footer type, Overtype mode, NumLock status, and more.

Category is Microsoft Office   (Article #12)
Comments

Hello...

I am having just a little bit of trouble with the one line of code for the page numbers...

When I use it, I seem to get all the page numbers the same which I am trying to work down, but more of an issue is that when I use it, I get Word memory errors. I have toggled this one line several times to make sure it was the actual line, and tested it with fresh reboots etc.

The only thing I can think of id maybe the information property is holding on the doc still when I go for the next one...

Any ideas would be very helpful.

I know it is strange to be using range in this way, but it seemed to be the only way I could get the type property which is not available above range. I am running this against W2K docs...

I am figuring I just adjusted incorrectly into the situation...

Thanks,
Peyton

PROC06:
If appWord.ActiveDocument.Fields.Count >= 1 Then
For Each tempRange In ActiveDocument.StoryRanges
For Each tempField In tempRange.Fields

tempPageNbr = 0
'tempPageNbr = tempRange.Information _
'(wdActiveEndPageNumber)
'Debug.Print tempPageNbr

If tempField.Type = 4 Then
' Here we kick out Index items for
' special processing...
GoSub PROC0601_NEWREC
TotalIndexItemCntr = TotalIndexItemCntr + 1
Else
' Here we grab all fields
' and stuff them in just in case...
GoSub PROC0699_NEWREC
End If
Next
Next
End If
Return

PROC0601_NEWREC:
GBLRST05.AddNew
GBLRST05!MDiitDocChapNbr = CURCHAP
GBLRST05!MDiitDocChapSubDirName = CURSUBDIR
GBLRST05!MDiitDocPath = CURDIR
GBLRST05!MDiitDocName = appWord.ActiveDocument.Name
GBLRST05!MDiitIndexItemLength = Len(tempField.Code)
GBLRST05!MDiitIndexItemPageNbr = tempPageNbr
If Len(tempField.Code) <= 255 Then
GBLRST05!MDiitIndexItem = tempField.Code
Else
GBLRST05!MDiitIndexItem = Left(tempField.Code, 255)
GBLRST05!MDiitIndexItemCat = "PROBLEM: Truncation"
GoTo PROC0601_SKIP
End If
' blah blah update blah blah
Return

Posted by: Peyton King at August 21, 2005 :30 AM

I have found a way to do this using Select, but it seems to put a serious drag on the system.

I looked for an unselect. I actually get the document flashing as a translucent ghost over the immediate window...

Neat effect, but I would prefer speed, LOL...

Thanks,
Peyton

Posted by: Peyton King at August 21, 2005 :58 AM
This site is incredible!
This site is incredible! It's amazing the questions you get asked and you always come up with the answer. I've just got to help you stay cool by buying you a Snoball!
I do have a comment, now that you mention it!









Remember personal info?


Please note that I will never send you any unsolicited
email, period. I hate it just as much as you do.

Also note that by submitting a question or comment
you're agreeing to relinquish any subsequent rights of
ownership to your material by submitting it on this site.









Ask Westley your question on technology, business, or politics!
RDF XML
Add to My Yahoo!
Subscribe in 
NewsGator Online
Feedburner
Search



All Categories
Archives by Category
Book Links
Powered by
Movable Type 3.17
© 2005 by Westley Annis. All Rights Reserved.

Valid XHTML 1.0!