Setting and retaining a range

H

HONYAKUKA

Word2000

I would like to perform a search-replace within a selected range of text (as
opposed to the whole document) and perform the same search repeatedly (within
the original range) until the target text-combination no longer exists.
(Specifically, "^p^p^p" to "^p^p", i.e., three contiguous paragraph marks to
two.)

I can't figure out how to retain the original selection (of course, the
"original" selection changes internally with each pass of the search-replace).
If you define a range as being the original selection, that named range no
longer refers to the original selection (as altered) once the search-replace
routine is run through the first pass. I tried defining the range by setting a
bookmark at the beginning and one at the end of the original selection, but the
search-replace routine destroys the end bookmark, so the orginally defined
range can no longer be selected.

Anyway, is there a foolproof way of defining the beginning and the end of the
original selection, so the range can be reselected and the search-replace
routine run till exhaustion? Thanks very much.

Jay
 
D

Denise Z

No problem. You just need to move the cursor to the
beginning of the range, set it, then move the cursor to
the end of the range and set it. Here's some code to get
you started:

'Cursor is at the beginning of range
Set StartRange = Selection.Range
'now move the cursor to the end of the range
Set StopRange = Selection.Range
'The next set of code blocks the range
Selection.SetRange Start:=StartRange.Start,
End:=StopRange.Start

You can play with this range til the cows come home until
you cancel the selection with a Selection.Collapse
wdCollapseStart or wdCollapseEnd

Hope this helps.

Denise
 
J

Jay Freedman

Hi Honyakuka,

Although this particular application has a simple solution, the answer to your
original question is interesting for other applications.

You can "save a copy" of the original range by using the .Duplicate property of
the Range object. This is a separate, independent range that happens to have the
same .Start and .End as the original. After you've done some operation that
moves or resizes the original, you can assign the copy back to it to restore the
location:

Sub foo()
Dim MyRange As Range, OtherRange As Range
Set MyRange = Selection.Range
Set OtherRange = MyRange.Duplicate

With MyRange
.Collapse Direction:=wdCollapseEnd
.Expand Unit:=wdCharacter
.Case = wdUpperCase
End With

Set MyRange = OtherRange
MyRange.Font.Italic = True
End Sub
 

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