Identify current page

B

BruceM

I have some code that is designed to print certain pages from a document.
The general idea is that the user will select a page, which will print along
with other pages in the document that are identified by means of bookmarks.
I am using this code to identify the first page:
ActiveDocument.ActiveWindow.Selection.Range.Information(wdActiveEndAdjustedPageNumber)
This code identifies the current page after the user clicks into it
(although yesterday I would have sworn all I had to do was go to it). Is
there a way to identify the page number simply by going to it? I know that
I can print the current page through code, but can I identify the current
page number?
 
J

Jay Freedman

BruceM said:
I have some code that is designed to print certain pages from a
document. The general idea is that the user will select a page, which
will print along with other pages in the document that are identified
by means of bookmarks. I am using this code to identify the first
page:
ActiveDocument.ActiveWindow.Selection.Range.Information(wdActiveEndAdjustedPageNumber)
This code identifies the current page after the user clicks into it
(although yesterday I would have sworn all I had to do was go to it).
Is there a way to identify the page number simply by going to it? I
know that I can print the current page through code, but can I
identify the current page number?

You're doing it right, although you can omit the
"ActiveDocument.ActiveWindow." part of the expression because the Selection
object always refers to the active selection or insertion point.

I don't understand what distinction you're making between "clicking into" a
page and "going to" a page. Either way, the Selection points to that page,
so it shouldn't make any difference how it got there.

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

BruceM

Thanks for the reply. I have removed the ActiveDocument.ActiveWindow part,
and it works as it did before. Can I do the same thing with
ActiveDocument.Bookmarks? Does it matter if the code is in an Add-In?

I may not have explained clearly, but here's the distinction I am making:
When I open a document the cursor is blinking at the very beginning of the
document. If I scroll to page 2, click Print, and choose to print the
current page, page 2 will print. However, if I do:

Dim lngPage as Long
lngPage = Selection.Range.Information(wdActiveEndAdjustedPageNumber)

lngPage will return "1", apparently because the insertion point remained on
page 1 even as I scrolled to page 2.
I am working on quite a number of documents, and have set up the message box
to test that I am printing the intended pages rather than actually printing
them. I have just realized I can print the current page using code no
matter where the cursor is located, so the fact that the message box shows
the "wrong" page number is in one sense not very important, but for other
reasons I would like to be able to obtain the number. The instruction to
print the current page:

ActiveDocument.PrintOut Range:=wdPrintCurrentPage

"knows" which page is visible on the screen. However, the following:

Dim lngPage as Long
lngPage = Selection.Range.Information(wdActiveEndAdjustedPageNumber)

returns the page where the insertion point is located as lngPage.

I work quite a lot with VBA Access, but the world of Word VBA is still
rather mysterious to me. Being unable to assign to a variable the page
number of the page currently in view is a minor problem, but I would like to
solve it anyhow, if possible.

BTW, I will be leaving work soon for the day, and will not return until
Monday. I may not have a chance to check the newsgroups, so if you reply
and I do not acknowledge it at first, please don't think me rude.
 
J

Jay Freedman

Thanks for the reply. I have removed the ActiveDocument.ActiveWindow part,
and it works as it did before. Can I do the same thing with
ActiveDocument.Bookmarks? Does it matter if the code is in an Add-In?

No. The Selection object is a property of the Application object (that
is, all of Word can have only one Selection at any time), and VBA lets
you omit the "Application." part for any of the Application's members.
That isn't true for bookmarks, as each document has its own Bookmarks
collection. So you do have to spell out ActiveDocument.Bookmarks (or,
if you've declared and assigned some other Document object, you can
then write something like MyDoc.Bookmarks).
I may not have explained clearly, but here's the distinction I am making:
When I open a document the cursor is blinking at the very beginning of the
document. If I scroll to page 2, click Print, and choose to print the
current page, page 2 will print. However, if I do:

Dim lngPage as Long
lngPage = Selection.Range.Information(wdActiveEndAdjustedPageNumber)

lngPage will return "1", apparently because the insertion point remained on
page 1 even as I scrolled to page 2.
I am working on quite a number of documents, and have set up the message box
to test that I am printing the intended pages rather than actually printing
them. I have just realized I can print the current page using code no
matter where the cursor is located, so the fact that the message box shows
the "wrong" page number is in one sense not very important, but for other
reasons I would like to be able to obtain the number. The instruction to
print the current page:

ActiveDocument.PrintOut Range:=wdPrintCurrentPage

"knows" which page is visible on the screen. However, the following:

Dim lngPage as Long
lngPage = Selection.Range.Information(wdActiveEndAdjustedPageNumber)

returns the page where the insertion point is located as lngPage.

Ahah, that is different. Unfortunately there are quite a few things in
the user interface of Word that VBA knows nothing about and can't
duplicate. Knowing the page number of a visible page when the
insertion point is somewhere else is one of those things. If for some
reason wdPrintCurrentPage isn't sufficient, then you're going to have
to click on a page to get the Selection to be there.
 
B

BruceM

Thanks for the information. I understand the distinction you are making
between Selection, which can apply to one document only, and the bookmarks
collection, which is document-specific.
Thanks too for the information about VBA "knowing" which page is current. I
suspected as much. It seems a curious thing that VBA can be used can
identify the current page for printing purposes, but can't tell anybody the
page number, but at least now I can stop looking for a way to do that. It
would have been mildly helpful, but it really doesn't matter. If it does,
the user will just have to click the page. It still would be a big
improvement over the way things are done now.
 

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