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 / March 2006

Tip: Looking for answers? Try searching our database.

How can I highlight text between cursor and find target?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Cleave - 29 Mar 2006 18:35 GMT
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.
Greg - 29 Mar 2006 18:48 GMT
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
Helmut Weber - 29 Mar 2006 19:12 GMT
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

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Cleave - 29 Mar 2006 22:42 GMT
> Hi Cleave,
>
[quoted text clipped - 20 lines]
> End With
> End Sub

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
Greg - 30 Mar 2006 03:41 GMT
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.
Jonathan West - 30 Mar 2006 10:39 GMT
> 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.

Signature

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

Greg Maxey - 30 Mar 2006 12:16 GMT
Thanks Jonathan.

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

>> Dick,
>>
[quoted text clipped - 20 lines]
> 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.
Cleave - 30 Mar 2006 15:47 GMT
Just what I was looking for!  Thank you!

Dick

> Thanks Jonathan.
>
[quoted text clipped - 22 lines]
> > 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.
 
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.