HighlightRepeatedWords Macro

🔍 Purpose of the Macro

It highlights repeated words in a Word document so you can avoid using them and to make your writing more professional.
Can be useful for writing any documents or thesis.

Direct repetitions – e.g., “the the”

Multiple occurrences in the same sentence – e.g., “He said that that was wrong” (will highlight both “that” if not an exception)

You have to launch the macro manually by pressing alt+f8 and selecting “HighlightRepeatedWords”. It scans the whole document!

🧱 How the Macro Is Built

1. Exceptions List

exceptions = Array("the", "a", "an", "and", "but", ...)

This is a list of common words you usually want to ignore when detecting repetition.

These are not highlighted even if repeated.

Setting Up the Document and Range

Set doc = ActiveDocument

Set rng = doc.Range

doc is the active Word document.

rng is the full text of the document to analyze.

3. Detecting Directly Repeated Words

For i = 1 To rng.Words.Count - 1

This loop checks each word against the next word (i and i + 1).

Converts both to lowercase and trims spaces.

rng.Words(i).HighlightColorIndex = wdYellow

rng.Words(i + 1).HighlightColorIndex = wdYellow

4. Detecting Repeated Words Within a Sentence

For Each sentence In rng.Sentences

Loops through each sentence in the document.

For each sentence, it creates a dictionary (wordsDict) to keep track of words already seen.

If a word shows up again in the same sentence and is not in the exceptions list:

The current and first occurrences are both highlighted.

5. Helper Function: IsException

Function IsException(word As String, exceptionList As Variant) As Boolean

This checks whether a word is in the list of exceptions.

Returns True if the word should be ignored.

6. Finishing

MsgBox "Highlighting completed.", vbInformation

A message box pops up when the macro finishes running.

Sample Text before:

It was a quiet quiet night in the village. The wind wind rustled through the trees, carrying with it a sound, a sound that no one could quite place. Sarah stared at the window, and at the same time, she stared at the past — a past that refused to stay buried. She thought she heard footsteps, footsteps echoing outside her door.

She shook her head. Maybe it was nothing. But the thought the thought kept returning. Something felt off, and she couldn't couldn't explain why. She had locked the doors, checked the windows, and turned off the lights. Still, she could not shake the feeling, the feeling that something — or someone — was near.

In the drawer, she kept a photo, a photo of a man she once knew, once loved. That man, that man had left without a word. Now, in the dark, she felt he was somehow back — back in a memory or back for real. Her heart beat faster, faster with each creak of the floorboards.

Was it paranoia? Or was it was it instinct? She didn’t know. All she knew was that the night the night wasn’t as silent as it seemed.

After:

It was a quiet quiet night in the village. The wind wind rustled through the trees, carrying with it a sound, a sound that no one could quite place. Sarah stared at the window, and at the same time, she stared at the past — a past that refused to stay buried. She thought she heard footsteps, footsteps echoing outside her door.

She shook her head. Maybe it was nothing. But the thought the thought kept returning. Something felt off, and she couldn't couldn't explain why. She had locked the doors, checked the windows, and turned off the lights. Still, she could not shake the feeling, the feeling that something — or someone — was near.

In the drawer, she kept a photo, a photo of a man she once knew, once loved. That man, that man had left without a word. Now, in the dark, she felt he was somehow back — back in a memory or back for real. Her heart beat faster, faster with each creak of the floorboards.

Was it paranoia? Or was it was it instinct? She didn’t know. All she knew was that the night the night wasn’t as silent as it seemed.

Another sample text but in Hungarian before:

Az az alma alma piros, és és a körte is sárga.

After:

Az az alma alma piros, és és a körte is sárga.