View Single Post
 
Old 10-10-2024, 10:22 AM
gmaxey gmaxey is offline Windows 10 Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,617
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Vika,


Speaking only for myself, I don't see posting alternative solutions as "hijacking" the post. I think we all learn from the experience and examples of others.



That said, I have always (and I don't really know why) have had an aversion to using "Selection" Here is another alternative that sort of combines Graham's collection and your process:

Code:
Dim oCol As New Collection
Dim clrfind
Dim oRng As Range, oRngSub As Range
  Application.ScreenUpdating = False
  Set oRng = ActiveDocument.Range
  With oRng.Find
    .ClearFormatting
    .Highlight = True
    .Wrap = wdFindStop
    While .Execute
      On Error Resume Next
      oCol.Add oRng.Text, oRng.Text
      If Err.Number = 0 Then
        clrfind = oRng.HighlightColorIndex
        Set oRngSub = ActiveDocument.Range
        oRngSub.Start = oRng.End
        With oRngSub.Find
           .Text = oRng
           .MatchCase = True
           .MatchWholeWord = True
           While .Execute
             oRngSub.HighlightColorIndex = clrfind
           Wend
         End With
         oRng.Collapse wdCollapseEnd
      Else
        Err.Clear
      End If
    Wend
  End With
  Application.ScreenUpdating = True
  Set oRng = Nothing: Set oRngSub = Nothing
 End Sub

Here we do use the collection key to bypass words that have already processed.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote