Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-17-2014, 09:01 PM
seascape seascape is offline Finding duplicate sentences in a document Windows 7 64bit Finding duplicate sentences in a document Office 2013
Novice
Finding duplicate sentences in a document
 
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
  #2  
Old 02-18-2014, 12:31 AM
macropod's Avatar
macropod macropod is offline Finding duplicate sentences in a document Windows 7 32bit Finding duplicate sentences in a document Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

The first thing you need to be aware of is that Word VBA's notion of what constitutes a sentence isn't what most readers would consider a sentence. For example, consider the following:

Mr. Smith spent $1,234.56 at Dr. John's Grocery Store, to buy: 10.25kg of potatoes; 10kg of avocados; and 15.1kg of Mrs. Green's Mt. Pleasant macadamia nuts.

For you and me, that would probably count as one sentence; for VBA it counts as 5... So what you're regarding as long sentences might not be the same as your macro's interpretation.

Given the above, I'd suggest a different approach, which is to use a two-dimensional array, with the 2nd dimension holding the 'sentence' index. Then, instead of using Find/Replace, simply retrieve the 'sentence' index when needed and highlight that 'sentence'. Just don't be surprised if some of the matches are only parts of grammatical sentences.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Tags
duplicate sentences, vba script



Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding duplicate sentences in a document Help with finding multiple keywords in a single document then highlight navyguy Word 2 01-03-2014 12:48 PM
Finding duplicate sentences in a document Finding or searching ^ character in word document shahin3121 Word 2 03-05-2012 06:16 PM
Finding duplicate sentences in a document keep only sentences beginning with: Wind Michael007 Word VBA 3 01-17-2011 04:11 PM
Finding where I last worked on a document. Balliol Word 1 11-24-2010 04:50 AM
Saving Word document creates a duplicate transparent file bbailey Word 0 12-05-2009 10:54 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:59 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft