View Single Post
 
Old 10-10-2024, 06:40 AM
vivka vivka is offline Windows 7 64bit Office 2016
Expert
 
Join Date: Jul 2023
Posts: 302
vivka is on a distinguished road
Default

Greg and Graham, let me put in my two cents' worth. I compiled the code, which may seem quite primitive, a while ago, but didn't want to hijack the thread for ethical reasons. Now that Greg posted his code, I think I will not hurt anyone's feelings.
Code:
Sub Hilite_All_Instances()
'In selection, hilite all plain occurrences of the hilited strs
'in their respective colors.

Dim oSel As range
Dim oRng As range
    Application.ScreenUpdating = False
    Set oSel = selection.range
    Set oRng = selection.range
    Do
        selection.range.Find.ClearFormatting
        selection.range.Find.Replacement.ClearFormatting
        With oRng.Find
            .Highlight = True
            If .Execute And oRng.InRange(selection.range) Then
                ClrFind = oRng.HighlightColorIndex
                Set oSel = selection.range
                With oSel.Find
                    .text = oRng
                    .MatchCase = True
                    .MatchWholeWord = True
                    .MatchWildcards = False
                    .Forward = True
                    .Wrap = wdFindStop
                    While .Execute And oSel.InRange(selection.range)
                        oSel.HighlightColorIndex = ClrFind
                    Wend
                End With
            Else: Exit Do
            End If
        End With
    Loop
    Application.ScreenUpdating = True
    Set oSel = Nothing
    Set oRng = Nothing
 End Sub
PS. The code doesn't use a collection, doesn't care about recoloring source words, and works on a selected range, which to my mind is more practical. To use it on a whole doc, one should press Ctrl+A before running the code. Greg's way of using a collection was new to me. Thank you, Greg! I live and learn.
Reply With Quote