Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / December 2004

Tip: Looking for answers? Try searching our database.

Selecting a range on a certain Page

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
John - 19 Dec 2004 02:48 GMT
Hello,
I am new to VBA with Word. Can someone tell me how I would select a certain
range on a specific page. For example, I am using the following to select a
range (from character 81 to the next tabstop). This seems  to work fine but
only on the 1st page of my document. How can I select this range on another
page, for example page 20?

Any help would be greatly appreciated! Thanks!

Set myRng = ActiveDocument.Range(Start:=81, End:=82)
myRng.Select

Do Until myRng = oChar

If myRng <> oChar Then
Pos = Pos + 1
Set myRng = ActiveDocument.Range(Start:=Pos, End:=Pos + 1)
myRng.Select
End If

Loop
Set myRng = ActiveDocument.Range(Start:=81, End:=Pos)
Jezebel - 19 Dec 2004 03:02 GMT
Not sure what you're getting at here. Ranges and pages are not directly
related. The body of the document is one big range, from Start = 1 to
whatever. If page 20 starts at position 593, then Range(Start:593, End:=594)
will refer to the first character on page 20.

You don't need to select the range to check its characters. In the code
snippet you give, the Select statements are irrelevant, apart from the last
one.

You don't need to redefine the range each time you iterate your loop: you
can simply reset range's start and end properties:

MyRange.Start = pos
MyRange.End = pos + 1

You can use the MoveEndUntil method to find the character you want in one
go:

With ActiveDocument.Range(Start:=81, End:=81)
   .MoveEndUntil oChar
   .End = .End + 1
   .Select
End with

> Hello,
> I am new to VBA with Word. Can someone tell me how I would select a certain
[quoted text clipped - 18 lines]
> Loop
> Set myRng = ActiveDocument.Range(Start:=81, End:=Pos)
John - 19 Dec 2004 03:20 GMT
Ok,
Thanks! But how can you tell what the starting character is for a given
page? In other words, how do you know what position page 20 starts at?

THANKS!

> Not sure what you're getting at here. Ranges and pages are not directly
> related. The body of the document is one big range, from Start = 1 to
[quoted text clipped - 48 lines]
>> Loop
>> Set myRng = ActiveDocument.Range(Start:=81, End:=Pos)
Jezebel - 19 Dec 2004 03:40 GMT
pStart =  Selection.GoTo(What:=wdGoToPage, Name:="20").Start

> Ok,
> Thanks! But how can you tell what the starting character is for a given
[quoted text clipped - 54 lines]
> >> Loop
> >> Set myRng = ActiveDocument.Range(Start:=81, End:=Pos)
fumei - 30 Dec 2004 17:35 GMT
Well, if you need to know the actual integer of the first character of any
page, you must go there first.  This is because "page" is a dynamic object.  
So:

 '  go to whatever page  - 20 in this example
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="20"
 ' uses the pre-defined bookmark "page" to get the Range.Start
 ' this is a Long Integer
MsgBox ActiveDocument.Bookmarks("\page").Range.Start

So, for example, if for some reason or other, you wanted to know how many
characters were on a specific page:

Dim lngStart As Long
Dim lngEnd As Long

Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="20"
lngStart = ActiveDocument.Bookmarks("\page").Range.Start
lngEnd = ActiveDocument.Bookmarks("\page").Range.End
MsgBox "This page starts at character number " & _
      lngStart & " and ends at " & _
      lngEnd & ", for a total of " & _
      (lngEnd - lngStart) & " characters, including spaces."

> pStart =  Selection.GoTo(What:=wdGoToPage, Name:="20").Start
>
[quoted text clipped - 59 lines]
> > >> Loop
> > >> Set myRng = ActiveDocument.Range(Start:=81, End:=Pos)

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.