
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, PROC06: 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, 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!
|
|