![]() |
|
|||||||
|
|
Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
|
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 |
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 |
Is this even possible: extrapolating highlighted words
|
angiesnow | Word | 2 | 08-12-2018 03:40 AM |
Macro in Word to track colour of highlighted text
|
BABZ | Word VBA | 1 | 01-09-2017 10:33 PM |