View Single Post
 
Old 02-17-2014, 09:01 PM
seascape seascape is offline Windows 7 64bit Office 2013
Novice
 
Join Date: Feb 2014
Posts: 1
seascape is on a distinguished road
Default Finding duplicate sentences in a document

Hi, this is my first post and I'm a VBA novice, so please have patience with me.

I'm trying to find and highlight duplicate sentences in a Word 2013 document, and I found this script http://stackoverflow.com/questions/1...-word-document.

Option Explicit

Sub Sample()
Dim MyArray() As String
Dim n As Long, i As Long
Dim Col As New Collection
Dim itm
n = 0
'~~> Get all the sentences from the word document in an array
For i = 1 To ActiveDocument.Sentences.Count
n = n + 1
ReDim Preserve MyArray(n)
MyArray(n) = Trim(ActiveDocument.Sentences(i).Text)
Next
'~~> Sort the array
SortArray MyArray, 0, UBound(MyArray)

'~~> Extract Duplicates
For i = 1 To UBound(MyArray)
If i = UBound(MyArray) Then Exit For
If InStr(1, MyArray(i + 1), MyArray(i), vbTextCompare) Then
On Error Resume Next
Col.Add MyArray(i), """" & MyArray(i) & """"
On Error GoTo 0
End If
Next i

'~~> Highlight duplicates
For Each itm In Col
Selection.Find.ClearFormatting
Selection.HomeKey wdStory, wdMove
Selection.Find.Execute itm
Do Until Selection.Find.Found = False
Selection.Range.HighlightColorIndex = wdPink
Selection.Find.Execute
Loop
Next
End Sub

However, on certain documents I get the "Run-time error '5854': String parameter too long." error on the "Selection.Find.Execute itm" command. Yet, when I look at the offending sentence, although it has more than 255 characters, there are many more, longer sentences which the macro processes normally. There are no hidden characters / formatting marks, and there are duplicate sentences after that particular sentence.

If I break the offending sentence in two, the macro runs fine. Any idea how I should modify the script?

Kind regards,
George
Reply With Quote