How can I highlight text between cursor and find target?

C

Cleave

I am very familiar with Basic (VBA) and am learning the ins and outs of the
Basic used with Word. I assume that one defines a variable in some way
related to a highlighted item.; I wish to highlight text between the cursor
and the target of a find.
 
G

Greg

Try:

Sub Test()
Dim oRng As Word.Range
Dim oHLRng As Word.Range
Set oRng = ActiveDocument.Content
Set oHLRng = oRng.Duplicate
With oRng.Find
.Text = "good"
.Execute
If .Found Then
oHLRng.Start = Selection.Range.Start
oHLRng.End = oRng.Start
oHLRng.HighlightColorIndex = wdYellow
End If
End With
End Sub
 
H

Helmut Weber

Hi Cleave,

like this, and in some other ways:

Sub test001264()
Dim sTmp As String ' a temporary string
Dim lPos As Long ' a position in the doc
Dim rTmp As Range ' a temporary range
sTmp = "quickx"

With Selection
.Collapse
lPos = .Start
Set rTmp = .Range
End With
With rTmp.Find
.Text = sTmp
If .Execute Then
rTmp.SetRange lPos, rTmp.End - Len(sTmp)
rTmp.Select ' for testing only
rTmp.HighlightColorIndex = wdYellow
End If
End With
End Sub

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
C

Cleave

Helmut Weber said:
Hi Cleave,

like this, and in some other ways:

Sub test001264()
Dim sTmp As String ' a temporary string
Dim lPos As Long ' a position in the doc
Dim rTmp As Range ' a temporary range
sTmp = "quickx"

With Selection
.Collapse
lPos = .Start
Set rTmp = .Range
End With
With rTmp.Find
.Text = sTmp
If .Execute Then
rTmp.SetRange lPos, rTmp.End - Len(sTmp)
rTmp.Select ' for testing only
rTmp.HighlightColorIndex = wdYellow
End If
End With
End Sub

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
Thanks, Greg and Helmut - I'll try these as I get deeper into it. But how
about this - in recording a macro (usually my first step at going to Basic)
how does one use the find and highlight the text between the cursor and
target? I've tried some key combinations but don't get the results.

Dick
 
G

Greg

Dick,

AFAIK, you can't get there from here. By using the recorder you are
only finding whatever it is that you put in the find box there is no
way that I know of to use keystrokes to define area between the found
text and the IP.
 
J

Jonathan West

Greg said:
Dick,

AFAIK, you can't get there from here. By using the recorder you are
only finding whatever it is that you put in the find box there is no
way that I know of to use keystrokes to define area between the found
text and the IP.

Actually you can do this. Pressing the F8 key or double-clicking the "EXT"
panel in the status bar puts you into Extend mode, where movements of the
cursor occur as if you were holding Shift down. If you then do a Find, all
the text between the original cursor position and the found text is
selected. Pressing ESC returns you to normal mode.

Recording a macro while doing this, you find that pressing F8 is equivalent
to this command

Selection.Extend

while pressing ESC is this command

Selection.EscapeKey

If you want to use this in association with a find, then you would need to
do the Find on the Selection object, not a Range.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 

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