Re: Deriving the word (range) at cursor location



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.

.



Relevant Pages

  • Re: Change selection color
    ... The background color scheme already is a dark blue. ... if you have PowerPoint 97 or 2000. ... >> the selection color is also white. ...
    (microsoft.public.powerpoint)
  • Re: Color Code
    ... > "I want to validate if background color is dark then only light colors ... > are in contrast with the background they have already chosen. ... >>color say he has selected the background color then the next selection ... f) Now if i give my background color has light and also my link color as ...
    (microsoft.public.inetserver.asp.general)
  • Re: Color Code
    ... following my conclusion both the background color code and the ... > are in contrast with the background they have already chosen. ... >>color say he has selected the background color then the next selection ...
    (microsoft.public.inetserver.asp.general)
  • Application wide setSelectionColor
    ... JTextPane lets me set the background color of selected text with the ... Though I don't offhand see how to do this ... selection colors lack the visual contrast my app requires. ...
    (comp.lang.java.gui)