I should have been clearer. When the user single left clicks inside a word,
I want to obtain the word (Range). I can't use Selection because nothing is
selected, only a the cursor's position inside a word.
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.
>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!

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
Jay Freedman - 03 Dec 2006 18:22 GMT
>>I should have been clearer. When the user single left clicks inside a
>>word,
[quoted text clipped - 17 lines]
>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.
MAB - 03 Dec 2006 20:06 GMT
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
> >>I should have been clearer. When the user single left clicks inside a
> >>word,
[quoted text clipped - 46 lines]
> Email cannot be acknowledged; please post all follow-ups to the
> newsgroup so all may benefit.
Jay Freedman - 03 Dec 2006 20:38 GMT
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.
>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
[quoted text clipped - 73 lines]
>> Email cannot be acknowledged; please post all follow-ups to the
>> newsgroup so all may benefit.
MAB - 03 Dec 2006 21:13 GMT
Good points. I'm starting to see the light a little.
Still though, if I select the 2nd of 2 of the same word, the 1st is not
found. It seem to be looking from the cursor's position to the end of the
document.
Private Sub App_WindowSelectionChange(ByVal Sel As Selection)
Dim sWord As String
Dim oRg As Range
sWord = Sel.Words(1).Text
Set oRg = ActiveDocument.Range
With oRg.Find
.Text = sWord
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = False
Do While .Execute
If oRg.Shading.BackgroundPatternColor = wdColorAqua Then
oRg.Shading.BackgroundPatternColor = wdColorAutomatic
End If
Loop
End With
End Sub
> I think you'll find that changing to
>
[quoted text clipped - 105 lines]
> >> Email cannot be acknowledged; please post all follow-ups to the
> >> newsgroup so all may benefit.
Jay Freedman - 04 Dec 2006 00:52 GMT
I think I know what's happening... Is the cursor in an occurrence of
the word that's followed by a space, while the one that isn't being
found is followed by a punctuation mark?
When you execute the line sWord = Sel.Words(1).Text and the word is
followed by a space, the space is included in the string sWord. Then
the Find won't match any occurrences that aren't followed by a space.
To fix this, change the line to
sWord = Trim(Sel.Words(1).Text)
The Trim function strips off any spaces at the beginning or end of the
string. Then the search will match all occurrences.
--
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.
>Good points. I'm starting to see the light a little.
>
[quoted text clipped - 131 lines]
>> >> Email cannot be acknowledged; please post all follow-ups to the
>> >> newsgroup so all may benefit.
MAB - 04 Dec 2006 14:48 GMT
U da man! Thank you so much for all the help Jay!
> I think I know what's happening... Is the cursor in an occurrence of
> the word that's followed by a space, while the one that isn't being
[quoted text clipped - 153 lines]
> >> >> Email cannot be acknowledged; please post all follow-ups to the
> >> >> newsgroup so all may benefit.
Jay Freedman - 05 Dec 2006 01:50 GMT
You're certainly welcome.
>U da man! Thank you so much for all the help Jay!
>
[quoted text clipped - 155 lines]
>> >> >> Email cannot be acknowledged; please post all follow-ups to the
>> >> >> newsgroup so all may benefit.
--
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.
MAB - 03 Dec 2006 18:29 GMT
Thanks for the help Jonathan & Jay
I was able to expand the selection with the following
l1 = Sel.MoveStart(wdWord, -1)
l2 = Sel.MoveEnd(wdWord, 1)
If Right(Sel.Text, 1) = " " Then l2 = Sel.MoveEnd(wdCharacter, -1)
however I would perfer not to expand the selection. I'd rather calulate a
range instead (how?).
Also, It seems the user cannot change the background color of a word that I
programmatically set early. Do you have any idea why?
> >I should have been clearer. When the user single left clicks inside a
> >word,
[quoted text clipped - 17 lines]
> Jay's code does give you the word in which the insertion point is flashing.
> Try it!
Jonathan West - 03 Dec 2006 18:39 GMT
> Thanks for the help Jonathan & Jay
>
[quoted text clipped - 8 lines]
> I
> programmatically set early. Do you have any idea why?
The Words collection is a collection of Range objects. Really, do please
just *try* Jay's code. You *don't* need to expand the selection in order to
get the whole word that contains the selection by means of
Selection.Words(1).

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