Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 10-08-2024, 12:31 AM
MUMS MUMS is offline A Word macro that highlights all other instances of words that are already highlighted Windows 8 A Word macro that highlights all other instances of words that are already highlighted Office 2016
Novice
A Word macro that highlights all other instances of words that are already highlighted
 
Join Date: Jul 2023
Posts: 8
MUMS is on a distinguished road
Default A Word macro that highlights all other instances of words that are already highlighted

This code was generated using Copilot AI but it's so slow it doesn't terminate:




Code:
Sub HighlightUnhighlightedInstancesOfHighlightedWords()
    Dim doc As Document
    Dim rng As Range
    Dim findText As String
    Dim highlightColor As Long
    Dim tempRng As Range
    Dim startPos As Long

    Set doc = ActiveDocument

    ' Loop through each word in the document
    For Each rng In doc.Words
        If rng.HighlightColorIndex <> wdNoHighlight Then
            ' Store the highlighted word and its highlight color
            findText = Trim(rng.Text)
            highlightColor = rng.HighlightColorIndex

            ' Find all instances of the highlighted word
            Set tempRng = doc.Content
            With tempRng.Find
                .ClearFormatting
                .Text = findText
                .Format = True
                .MatchWholeWord = True
                .MatchCase = False
                .MatchWildcards = False
                .MatchSoundsLike = False
                .MatchAllWordForms = False
                .Forward = True
                .Wrap = wdFindStop

                Do While .Execute
                    ' Check if the found instance is not already highlighted
                    If tempRng.HighlightColorIndex = wdNoHighlight Then
                        tempRng.HighlightColorIndex = highlightColor
                    End If
                    ' Move the range past the found instance
                    startPos = tempRng.End
                    tempRng.Collapse wdCollapseEnd
                    tempRng.Start = startPos
                    tempRng.End = doc.Content.End
                Loop
            End With
        End If
    Next rng
End Sub

Last edited by macropod; 10-08-2024 at 02:23 PM. Reason: Added code tags
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy highlighted words from Word Document to designated Excel file syl3786 Word VBA 2 07-26-2023 11:56 PM
A Word macro that highlights all other instances of words that are already highlighted How to use Word Macro to change all highlighted words as mark-up? hcl75 Word VBA 3 10-08-2022 02:39 PM
How to point a macro that highlights from a string of words at the footnotes rather than main doc poggyton Word VBA 4 10-15-2019 06:19 PM
A Word macro that highlights all other instances of words that are already highlighted Is this even possible: extrapolating highlighted words angiesnow Word 2 08-12-2018 03:40 AM
A Word macro that highlights all other instances of words that are already highlighted Macro in Word to track colour of highlighted text BABZ Word VBA 1 01-09-2017 10:33 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:19 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft