How to move the cursor to the end of a range?

C

cyberdude

Hi,

A visitor here gave me the following code to highlight a block of
words ending with "test" and delete it.

Sub Testb()
Dim x As Long
Dim rDcm As Range
Set rDcm = Selection.Range
x = rDcm.start
rDcm.End = ActiveDocument.Range.End
With rDcm.Find
.Text = "test"
If .Execute Then
rDcm.start = x
rDcm.HighlightColorIndex = wdYellow
rDcm.Delete
End If
End With
End Sub

If I remove the line "rDcm.Delete", then the code highlights the block
of text without deleting it. I would like to further change it so
that the cursor stops behind the word "test". Could someone tell me
how to do that? Thanks.

Mike
 
C

Cindy M.

Hi Cyberdude,
If I remove the line "rDcm.Delete", then the code highlights the block
of text without deleting it. I would like to further change it so
that the cursor stops behind the word "test". Could someone tell me
how to do that?
I'm not sure which side of the word "behind" is but either

rDcm.Collapse wdCollapseStart 'the left
or
rDcm.Collapse wdCollapseEnd 'the right

But please note for the first of the two that you should avoid
rDcm.Start = x, otherwise you'll end up at the start of the original
range.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 
S

StevenM

To: Cyberdude,

How about something like:

Sub TestFindOtherSideOfWord()
Dim newDoc As Document
Dim newRange As Range

Set newDoc = Documents.Add
Set newRange = newDoc.Range(0, 0)
newRange.Text = "Once upon a time, there was a test. Afterwards ..."
If FindOtherSideOfWord(newRange, "test") Then
newRange.Select
End If
End Sub

Function FindOtherSideOfWord(ByRef oRange As Range, ByVal sStr As String) As
Boolean
Dim findRange As Range

Set findRange = oRange.Duplicate
findRange.Collapse wdCollapseStart
With findRange.Find
.Forward = True
.Wrap = wdFindStop
.Text = sStr
.Execute
End With
If findRange.Find.Found = True Then
findRange.Collapse wdCollapseEnd
Set oRange = findRange.Duplicate
End If
FindOtherSideOfWord = findRange.Find.Found
End Function

Steven Craig Miller
 
D

Doug Robbins - Word MVP

If by that you mean select the text upto and including the word "test", use

Dim range1 As Range
Set range1 = ActiveDocument.Range
range1.End = range1.start + InStr(range1, "test") + 3
range1.Select


--
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
 

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