Friday, September 30, 2011

Detect Duplicate words in sentences

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
If InStr(1, strAllWords, vbTab & strWd & vbTab) > 0 Then
wd.Font.Color = wdColorRed
strAllWords = strAllWords & strWd & vbTab
End If
End If
Else ' we ignore words of length 1 character
End If
Next wd
End Function

No comments:

Post a Comment