Range Roving

R

Ridge Kennedy

Dear Group,

Inspired by comments from one of you about the value of using range vs.
selection for working with Word, I've been working to get a grasp of how to
do that. (It's sooo much easier for me to get record it on the keyboard
don'cha know). MS and most of the documents I've found tell me how to
identify the first or third paragraph, beginning or end of document, stuff
like that. Straightforward, but not always the useful information I thought
I needed.

Following is code for three subs. The first just finds the current cursor
location (or start of a current selection). The latter two identify the
paragraph where the cursor is currently located and set that "current"
paragraph as a range.

I'd appreciate comments, corrections and recommendations for improvements.

I'd also appreciate suggestions for strategy on a way to select the current
*line* as a range. Select properties (or are they methods -- I still don't
grock that) make it fairly easy to select a single line in the document. I
haven't figured out how to do it with range.

Any other useful tips on manipulating ranges also greatly appreciated.

Here are my subs (novice code writer -- be gentle):

Sub findCurrSelLocation()

' find the character number of the current
' cursor location or start of current selection
Dim lPosNow As Long
lPosNow = Selection.Range.Start

MsgBox ("The insertion point" & vbCrLf & "(or selection area)" _
& vbCrLf & "is at character location:" & lPosNow)

End Sub


Sub SetCurrParagraphAsRange()
' procedure to set the paragraph where the cursor
' is currently located as myrange

Dim myrange As Range
Dim lPos1 As Long
Dim lPos2 As Long
Dim oDoc As Object

Set oDoc = ActiveDocument

lPos1 = Selection.Paragraphs(1).Range.Start
lPos2 = Selection.Paragraphs(1).Range.End

Set myrange = oDoc.Range(Start:=lPos1, End:=lPos2)

' run sub showrange that changes the text color of myrange
' to verify where range is set
showrange myrange

End Sub


Sub SetCurrParagraphAsRangeII()

' procedure to identify the index number of the
' current paragraph and set it as myrange

Dim myrange As Range
Dim oDoc As Object
Dim lPosNow As Long
Dim lParCount As Long
Dim lStartDoc As Long

Set oDoc = ActiveDocument
Set myrange = oDoc.Content

' set myrange from start of document to
' current cursor location

lStartDoc = myrange.Start
lPosNow = Selection.Range.Start

Set myrange = oDoc.Range(Start:=lStartDoc, End:=lPosNow)

' store the count of paragraphs in myrange in lParCount

lParCount = myrange.Paragraphs.Count

' redefine myrange using the count to select
' the currently selected paragraph

Set myrange = oDoc.Paragraphs(lParCount).Range

' run sub that changes the text color of myrange
' to verify where range is set
showrange myrange

End Sub

Thanks. Ridge (in New Joisey[Exit 145])
 
D

Doug Robbins

Better (at least a damn sight less code) to use

Dim myrange as Range
Set myrange = Selection.Paragraphs(1).Range

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
Ridge Kennedy said:
Dear Group,

Inspired by comments from one of you about the value of using range vs.
selection for working with Word, I've been working to get a grasp of how
to
do that. (It's sooo much easier for me to get record it on the keyboard
don'cha know). MS and most of the documents I've found tell me how to
identify the first or third paragraph, beginning or end of document, stuff
like that. Straightforward, but not always the useful information I
thought
I needed.

Following is code for three subs. The first just finds the current cursor
location (or start of a current selection). The latter two identify the
paragraph where the cursor is currently located and set that "current"
paragraph as a range.

I'd appreciate comments, corrections and recommendations for improvements.

I'd also appreciate suggestions for strategy on a way to select the
current
*line* as a range. Select properties (or are they methods -- I still don't
grock that) make it fairly easy to select a single line in the document. I
haven't figured out how to do it with range.

Any other useful tips on manipulating ranges also greatly appreciated.

Here are my subs (novice code writer -- be gentle):

Sub findCurrSelLocation()

' find the character number of the current
' cursor location or start of current selection
Dim lPosNow As Long
lPosNow = Selection.Range.Start

MsgBox ("The insertion point" & vbCrLf & "(or selection area)" _
& vbCrLf & "is at character location:" & lPosNow)

End Sub


Sub SetCurrParagraphAsRange()
' procedure to set the paragraph where the cursor
' is currently located as myrange

Dim myrange As Range
Dim lPos1 As Long
Dim lPos2 As Long
Dim oDoc As Object

Set oDoc = ActiveDocument

lPos1 = Selection.Paragraphs(1).Range.Start
lPos2 = Selection.Paragraphs(1).Range.End

Set myrange = oDoc.Range(Start:=lPos1, End:=lPos2)

' run sub showrange that changes the text color of myrange
' to verify where range is set
showrange myrange

End Sub


Sub SetCurrParagraphAsRangeII()

' procedure to identify the index number of the
' current paragraph and set it as myrange

Dim myrange As Range
Dim oDoc As Object
Dim lPosNow As Long
Dim lParCount As Long
Dim lStartDoc As Long

Set oDoc = ActiveDocument
Set myrange = oDoc.Content

' set myrange from start of document to
' current cursor location

lStartDoc = myrange.Start
lPosNow = Selection.Range.Start

Set myrange = oDoc.Range(Start:=lStartDoc, End:=lPosNow)

' store the count of paragraphs in myrange in lParCount

lParCount = myrange.Paragraphs.Count

' redefine myrange using the count to select
' the currently selected paragraph

Set myrange = oDoc.Paragraphs(lParCount).Range

' run sub that changes the text color of myrange
' to verify where range is set
showrange myrange

End Sub

Thanks. Ridge (in New Joisey[Exit 145])
 
R

Ridge Kennedy

Doug Robbins said"
Dim myrange as Range
Set myrange = Selection.Paragraphs(1).Range
Hope this helps.

Absolutely. It's a learning process.

Is there a similar, elegantly simple solution to setting the range to just
the line where an insertion point is located?

Ridge
 
J

Jay Freedman

Doug Robbins said"



Absolutely. It's a learning process.

Is there a similar, elegantly simple solution to setting the range to just
the line where an insertion point is located?

Ridge

The insertion point is associated with a group of built-in bookmarks
named "\word", "\line", "\page", and "\section". So the line
containing the insertion point is

Set myRange = ActiveDocument.Bookmarks("\line").Range

There isn't anything nearly so easy for retrieving the line of an
arbitrary range, unfortunately.
 
J

Jay Freedman

The insertion point is associated with a group of built-in bookmarks
named "\word", "\line", "\page", and "\section". So the line
containing the insertion point is

Set myRange = ActiveDocument.Bookmarks("\line").Range

There isn't anything nearly so easy for retrieving the line of an
arbitrary range, unfortunately.

Oops, forgot "\para", "\sel", and a bunch of others. See the VBA help
topic on "Predefined bookmarks" for the whole list.
 
J

Jezebel

You can get to lines via code like --

Dim objLine as Word.Line
Set objLine = ActiveDocument.ActiveWindow _
.Panes(1).Pages(1).Rectangles(1).Lines.Item(1)

But I suggest you don't go there for code that modifies the document. Pages
and Rectangles (new with W2003, I think) are mysterious beasts, to say the
least, intended apparently for holistic formatting purposes.
 
R

Ridge Kennedy

Jay Freedman said:
The insertion point is associated with a group of built-in bookmarks
named "\word", "\line", "\page", and "\section".

I tried this and it worked great. Then per following, I tried to redefine it
to grab a different line while in the same procedure, and it kept returning
the first (original) line (as a range) that I had selected. I struggled with
it for a while and finally guessed that I have to find some way to re-set
the bookmark, so then I can re-define it as a second line. So I've been
poking around in bookmarks looking for a way to "reset" them.

Here's the test code I am working with:

Public Sub SetLineAsRange()
Dim myrange As Range
' set myrange to the line where the insertion point is
Set myrange = ActiveDocument.Bookmarks("\line").Range
' run sub to show the range that is selected
showrange myrange
' set myrange to the entire paragraph
Set myrange = Selection.Paragraphs(1).Range
showrange myrange
' set myrange to the start of the paragraph
' started using .collapse wdCollapseStart
' which I assume is preferred -- this seemed to work too
myrange.Start = myrange.End
showrange myrange
' try to select the first line of the paragraph
Set myrange = ActiveDocument.Bookmarks("\line").Range
showrange myrange
' nope -- goes back to the originally selected line
End Sub
 
R

Ridge Kennedy

Jezebel said:
Dim objLine as Word.Line
Set objLine = ActiveDocument.ActiveWindow _
.Panes(1).Pages(1).Rectangles(1).Lines.Item(1)

But I suggest you don't go there for code that modifies the document.

Do you think it would be acceptabel to use for copying a line and storing it
as a string variable to be used later for modifying (being inserted)
elsewhere in the document?

I'll play with it. Thank you.

R.
 
J

Jay Freedman

Jay Freedman said:


I tried this and it worked great. Then per following, I tried to redefine it
to grab a different line while in the same procedure, and it kept returning
the first (original) line (as a range) that I had selected. I struggled with
it for a while and finally guessed that I have to find some way to re-set
the bookmark, so then I can re-define it as a second line. So I've been
poking around in bookmarks looking for a way to "reset" them.

Here's the test code I am working with:

Public Sub SetLineAsRange()
Dim myrange As Range
' set myrange to the line where the insertion point is
Set myrange = ActiveDocument.Bookmarks("\line").Range
' run sub to show the range that is selected
showrange myrange
' set myrange to the entire paragraph
Set myrange = Selection.Paragraphs(1).Range
showrange myrange
' set myrange to the start of the paragraph
' started using .collapse wdCollapseStart
' which I assume is preferred -- this seemed to work too
myrange.Start = myrange.End
showrange myrange
' try to select the first line of the paragraph
Set myrange = ActiveDocument.Bookmarks("\line").Range
showrange myrange
' nope -- goes back to the originally selected line
End Sub

The predefined bookmarks are always tied to *where the Selection is*
at the time you look at the bookmark. Since you didn't move the
Selection, you don't get any other line -- no matter what you do with
myrange.

To get some other line, move the Selection to that line first, and
then get .Bookmarks("\line").Range.

That's why I don't particularly like this stuff -- moving the
Selection also moves the highlight on the screen, and may cause the
screen to scroll. That can be disconcerting for the user, and can make
the macro slower by an order of magnitude. When you're dealing with
words, paragraphs, sections, and tables, it's much better to
manipulate the Range object without touching the Selection at all. In
versions before 2003, though, VBA doesn't have a .Pages or .Lines
collection so you have to use the Selection for those.
 
P

Pat L

:

Sub SetCurrParagraphAsRange()
' procedure to set the paragraph where the cursor
' is currently located as myrange

Dim myrange As Range
Dim lPos1 As Long
Dim lPos2 As Long
Dim oDoc As Object

Set oDoc = ActiveDocument

lPos1 = Selection.Paragraphs(1).Range.Start
lPos2 = Selection.Paragraphs(1).Range.End

Set myrange = oDoc.Range(Start:=lPos1, End:=lPos2)

' run sub showrange that changes the text color of myrange
' to verify where range is set
showrange myrange

End Sub


Whenever I need to do this I simply do this:

dim rg as range
set rg=selection.range
rg.expand unit:=wdparagraph
 

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