I am having trouble with a macro that is supposed to go through the Word document (Word 2016) and remove all of the yellow highlighting (just yellow), and then show a message box with how many highlights were removed. The issue is that when I have highlighting in a table, the macro gets stuck in an infinite loop and the program has to be closed, losing any unsaved work.
Code:
Sub RemoveYellowHighlightingCount()
Dim myInt As Integer
myInt = 0
Selection.HomeKey unit:=wdStory
With Selection.Find
.ClearFormatting
.Highlight = True
Do While (.Execute(Forward:=True) = True) = True
If Selection.Range.HighlightColorIndex = wdYellow Then
Selection.Range.HighlightColorIndex = wdAuto
Selection.Collapse Direction:=wdCollapseEnd
myInt = myInt + 1
End If
Loop
End With
Select Case myInt
Case 0
MsgBox "No yellow highlighted ranges were found."
Case 1
MsgBox "One yellow highlighted range was removed."
Case Is > 1
MsgBox myInt & " yellow highlighted ranges were removed."
End Select
End Sub
if you want some help, post a sample document that demonstrates the problem
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
I have attached a document with the macro already in it. I would suggest manually walking through the macro, otherwise it will lock up Word. There is a second macro included, which we use to insert in the green Data Pendings.
i tested your code (as posted above), with the sample doc worked ok, no error, finished correctly
please note, this was run in an earlier version of word, so some differences might apply. i will try to test again in a later version
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete