VBA Script for MS Word: Highlight All Words in List

Mar 16, 2019 |

As I was entering the final rounds of detailed editing of my dissertation, a >400 page document, I needed some additional tools to help accelerate the editing process.

I developed the following VBA code to highlight words. The goal was to audit verb tenses. However, this could be adapted to any set of words.

Installing this is as easy as copying and pasting it into a new module in Word VBA. You can learn to do this in 15 minutes of work. Learn it! You will save yourself hours of frustration.

All you need to do to customize this list is to edit the highlightlist function at the end.


Sub find_words_in_doc()
Dim i, j As Integer
Dim wordList As Variant
Dim listLength As Integer
Dim isBad As Boolean
Dim para As Paragraph
Dim wordCount, currWord, editCount As Integer
On Error GoTo ErrorHandle
currWord = 0

wordCount = ActiveDocument.Words.Count
editCount = 0

' Set to a dictionary list
'See below for this code
wordList = highlightlist 'highlightlist is the function that stores dictionaries

'Set Length
listLength = UBound(wordList) - LBound(wordList)

'Search each word
For Each para In ActiveDocument.Range.Paragraphs
For Each wrd In para.Range.Words
isBad = False
For i = 0 To listLength
'tests trimmed word against bad word list
If Trim(wrd.Text) = wordList(i) Then
wrd.Select
'selects and highlights word
Selection.Range.HighlightColorIndex = wdBrightGreen
editCount = editCount + 1
isBad = True
End If
Next i
'for progress bar
currWord = currWord + 1
Next wrd
'prints progress
'Debug.Print currWord / wordCount
Next para

ErrorHandle:
'Application.ScreenUpdating = True
Application.StatusBar = False

MsgBox editCount & " words highlighted."

End Sub

Function highlightlist() As Variant

'Assign highlightlist a value as an array. I have two examples below. It's possible to make this set as long as needed.
'Note the syntax for the list: Array("word1", "word2") Each word should be in quotes and separated by commas.

highlightlist = Array("uses", "says", "argues", "writes", "states", "mentions", _
"claims", "considers", "believes", "contemplates", "expects", "distinguishes", "does", "does not", _
"condemns", "points", "calls", "parts", "prefers", "lacks", "has")

'highlightlist = Array("is", "encourages", "categorizes")

End Function

Posted in: Uncategorized

Comments are closed.