709-218-7927

The Landfall Garden House

60 Canon Bayley Road

CANADA A0C 1B0

CPRGreaves@gmail.com

Home

Christopher Greaves

Detect Duplicate Words in Sentences

Friday, September 30, 2011

This little macro detects duplicated words in sentences.

Unlike Microsoft Word's grammar/spell checker which merely identifies consecutive duplicate words, this macro (and associated function) marks in RED second and subsequent occurrences of words in each sentence.

A short list of noise words is provided as a string constant; we don't mind those words being repeated.

Public Const strcIgnoreWords As String = vbTab & "and" & vbTab & "on" & vbTab & "the" & vbTab

Sub DuplicateWordsInSentences()

Dim prg As Paragraph

For Each prg In ActiveDocument.Paragraphs

Dim snt As Range

For Each snt In prg.Range.Sentences

Call FindDupInSentence(snt)

Next snt

Next prg

End Sub

Function FindDupInSentence(snt As Range)

Dim strAllWords As String

strAllWords = vbTab

Dim wd As Range

For Each wd In snt.Words

Dim strWd As String

strWd = Trim(wd.Text)

If Len(strWd) > 1 Then

If InStr(1, strcIgnoreWords, vbTab & strWd & vbTab) > 0 Then ' we ignore this word

Else

If InStr(1, strAllWords, vbTab & strWd & vbTab) > 0 Then

wd.Font.Color = wdColorRed

Else

strAllWords = strAllWords & strWd & vbTab

End If

End If

Else ' we ignore words of length 1 character

End If

Next wd

End Function

7092187927 CPRGreaves@gmail.com

Bonavista, Friday, December 04, 2020 5:54 PM

Copyright © 1996-2020 Chris Greaves. All Rights Reserved.