Re: Deriving the word (range) at cursor location
- From: Jay Freedman <jay.freedman@xxxxxxxxxxx>
- Date: Sun, 03 Dec 2006 15:38:16 -0500
I think you'll find that changing to
oRg.HighlightColorIndex = wdAuto
as I said in my reply to your other post will fix this, too. It worked
OK when I tested it. But...
Besides that issue, you have some mistakes and unnecessary things in
this version of the code that may be masking the problem.
1. Remove the If Sel.Type statement, and just set sWord =
Sel.Words(1).Text regardless of the type. The reason is that if your
selection is anything other than an insertion point when this macro is
called, sWord will be an empty string ( = "" ) and the Find won't do
anything.
2. The statement oRg.Start = 1 is unnecessary. When you set oRg =
ActiveDocument.Range, that implicitly sets oRg.Start to 1 and oRg.End
to the last character position.
3. When you set .Format = True, that's telling Word to use whatever
formatting has been defined for oRg as a search criterion -- but you
call the .ClearFormatting method and don't supply any other
formatting. so the .Format = True is useless. Leave it as False.
--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
On Sun, 3 Dec 2006 12:06:01 -0800, MAB <MAB@xxxxxxxxxxxxxxxxxxxxxxxxx>
wrote:
If I have the same word twice in the doc and I select the second one, only.
the second one's background color changes. It seems even though the range
for the find is the entire document, the selection's cursor position somehow
controls the starting point of the search.
Private Sub App_WindowSelectionChange(ByVal Sel As Selection)
Dim sWord As String
Dim oRg As Range
If Sel.Type = wdSelectionIP Then
sWord = Sel.Words(1).Text
Set oRg = ActiveDocument.Range
oRg.Start = 1
With oRg.Find
.ClearFormatting
.Text = sWord
.Forward = True
.Wrap = wdFindStop
.Format = True ' 'False
.MatchWildcards = False
Do While .Execute
oRg.Shading.BackgroundPatternColor = wdColorAutomatic
Loop
End With
End If
End Sub
"Jay Freedman" wrote:
On Sun, 3 Dec 2006 17:24:34 -0000, "Jonathan West" <jwest@xxxxxxxx>
wrote:
"MAB" <MAB@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:A49660B6-1572-4A16-B1B7-C909A7B91B47@xxxxxxxxxxxxxxxx
I should have been clearer. When the user single left clicks inside a
word,
I want to obtain the word (Range).
You were perfectly clear
I can't use Selection because nothing is
selected, only a the cursor's position inside a word.
The Selection object exists even when you have a flashing cursor. The Type
property of the Selection object tells you what sort of selection it is.
wdSelectionIP referes to the flashing insertion point.
If the user single left clicks on a word with a certain barckground color
(previously applied programmatically by another app of mine), I want to
remove the background color for all instances of that word. Once I obtain
the word, I can iterate thru the document's Words collection to remove all
instances.
Jay's code does give you the word in which the insertion point is flashing.
Try it!
MAB,
Following on from your further description, please don't iterate
through the Words collection -- it's almost the slowest possible way
(only iterating through the Characters collection is slower). Instead,
once you have the word, use a Find operation in a loop to search the
document:
Dim oRg As Range
Set oRg = ActiveDocument.Range
With oRg.Find
.ClearFormatting
.Text = Selection.Words(1).Text
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = False
Do While .Execute
oRg.Shading.BackgroundPatternColor = wdColorAutomatic
Loop
End With
--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
- Follow-Ups:
- References:
- Re: Deriving the word (range) at cursor location
- From: Jay Freedman
- Re: Deriving the word (range) at cursor location
- From: Jonathan West
- Re: Deriving the word (range) at cursor location
- From: Jay Freedman
- Re: Deriving the word (range) at cursor location
- From: MAB
- Re: Deriving the word (range) at cursor location
- Prev by Date: Re: Deriving the word (range) at cursor location
- Next by Date: Re: Deriving the word (range) at cursor location
- Previous by thread: Re: Deriving the word (range) at cursor location
- Next by thread: Re: Deriving the word (range) at cursor location
- Index(es):
Relevant Pages
|