Find user-chosen pages in a document

J

James

If the user asks for pages x to y, how can I find page x (then maybe do
some formatting), then move on until I hit page y?
 
H

Helmut Weber

Hi James,
I know, newsreaders are unreliable, some show this and some show that.
This is a copy of my previous answer:
....like this:
Sub Test633()
Dim x As Integer ' page to start with
Dim y As Integer ' page to end with
Dim i As Integer ' a counter
x = 13 ' you can handle this input by an inputbox
y = 17 ' or by a userform, e.g.?
For i = x To y
Selection.GoTo _
what:=wdGoToPage, _
which:=wdGoToAbsolute, _
Count:=i
Selection.Bookmarks("\page").Select
stop ' do what you like to the selection
Next
End Sub
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
Gruss
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
J

James

A follow-up: Once I am in one of the selected pages, how would I be
able to find and select and reference the tables in the selection? I am
apologize for so many newbie questions, but documentation on Word VBA
is pretty limited.
Tks.
 
H

Helmut Weber

Hi James,
Once a page is selected, you get information on that page by e.g.
Msgbox selection.tables.count
But a table is in a selection (Word's definition),
if the selection is at least partly in the table!
Which means, that not all of a table must be in the selection,
in order to belong to the selection.
(We all went through this). ;-)
If no table on that page reaches to another page,
then the first table on the page would be selection.tables(1).
The last table on that page would be selected by:
selection.tables(selection.tables.count).select
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
J

James

Where you have stop in the above code I put the following:
tblCnt = Selection.Tables.Count
For cnt = 1 To tblCnt
Set atable = Selection.Tables(cnt)
Debug.Print "# of rows : " & atable.Rows.Count
Debug.Print "page #: " &
Selection.Information(wdActiveEndAdjustedPageNumber)

The page # is returns seems to skip one of the pages - page 3 has a
table but it prints out like it is on page 4. Is selection.information
the best way to get the page # of the current selection?
 
H

Helmut Weber

Hi James,
first, you don't need
Selection.Information(wdActiveEndAdjustedPageNumber)
You already know, on which page you are.
Selection.GoTo _
what:=wdGoToPage, _
which:=wdGoToAbsolute, _
Count:=i
You are on page i.
Second, the insertion point and the selection behave strange,
sometimes. They count units, even if they only touch them.
Example:
Selection.Collapse
MsgBox Len(Selection.Text) ' 1
Selection.MoveRight _
Unit:=wdCharacter, _
Count:=1, _
Extend:=wdExtend
MsgBox Len(Selection.Text) ' 1, too
So, whether there is a character selected or not,
the length of the selection is 1!
If all of a page is selected,
the selection touches the next page in a way.
On the last page of a document
Selection.Bookmarks("\page").Select
doesn't select all of the page, but leaves the last
paragraph mark unselected.
Not too much logic in it, IMHO.
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
J

James

I am having problems with the above solution
(Selection.Bookmarks("\page").Select) when the table splits across
pages. It picks up the table twice, once in the first page then again
in the second page.
Is this a known issue?

Is there a work-around?

Tks,
James
 

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