# Solved: Macro that finds and highlights specific words in MS Word document



## enars (Jul 10, 2008)

I need a macro that finds a specific word in a text and highlights it. For example, I would like to find a word "computer" in some text and make all words "computer" in red color. Can anybody help me to create such macro for Ms Word?


----------



## Aj_old (Sep 24, 2007)

hi and welcome!
You don't need a macro for this!
Go to MS Word, Ctrl+H (or Edit\Replace... ), in the field Find What type your text, for example "Computer"
in the field Replace with type nothing (leave it empty),press the More Button, put the cursor in it Replace With field, and press the button Format, and select Highlight option (bellow the Replace With field you'll see the text "Format: Highlight"
Now press the Replace All button!


----------



## enars (Jul 10, 2008)

Hi Aj_old,

Thanks for reply! I followed your instructions and recorded a macro that looks like this:

* Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.highlight = True
With Selection.Find
.Text = " "" "
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Options.DefaultHighlightColorIndex = wdYellow
Selection.Range.HighlightColorIndex = wdYellow*

In this example it highlights any quotation mark (") that has spaces before and after. I use it for checking spelling errors and deciding which space is unnecessary.

Regards,

Enars  http://www.hotelbooklet.com


----------



## Aj_old (Sep 24, 2007)

Hi glad that my post helped!
You can replace the line 

```
.Text = " "" "
```
in your code with this one:

```
.Text = Inputbox("Type in the text you wanna be highlighted")
```
so you could do the same with any text, without having to modify each time your code!


----------



## Aj_old (Sep 24, 2007)

If it's all you can mark thread as solved, using thread tools


----------



## Plantsman (Jan 20, 2008)

if you want to highlight general spelling errors here's a Macro for that also.

The following macro highlights any misspelled words:


Sub HighlightMisspelledWords()
Dim oWord As Range
Dim StoryRange As Range
For Each StoryRange In ActiveDocument.StoryRanges
Application.CheckSpelling Word:=StoryRange
For Each oWord In StoryRange.Words
If Not Application.CheckSpelling(Word:=oWord.Text) Then
oWord.HighlightColorIndex = wdYellow
End If
Next oWord
Next StoryRange
End Sub



Clear all highlighting from a document.
The following macro clears highlighting from all words:


Sub ClearHighlightFromAllWords()
Dim StoryRange As Range
For Each StoryRange In ActiveDocument.StoryRanges
StoryRange.HighlightColorIndex = wdNoHighlight
Next StoryRange
End Sub


----------

