G
Greg Maxey
I came across an old post in Google groups for deleted duplicated
lines of text in a document.
It used a For x = Count method to go through and check the range of
one paragraph to the the range of every other paragraph and delete any
duplicates.
It had two If ... End If blocks. The first check the para range
length. If = then the second performed a text comparison. I assume
the author thought that it would save time by doing a text comparison
only on paras of equal length.
The procedure worked as advertised, however with a longer document it
took a long time.
I created about 800 paragraphs and determined that it was actually
much quicker to bypass the the first length check and just do a range
comparison on every paragraph. Down from 200 seconds to 75 seconds!
Next I remembered an method that Jezebel showed me for stepping
through items using the .Next (property or method I am never sure
which).
I adapted the code as follows and the time taken was down to 3
seconds!
Anyway, I just wanted to share this with the group:
Sub KillDuplicateParagraphs()
Dim SBar As Boolean
Dim TrkStatus As Boolean
Dim eTime As Single
Dim oParRef As Paragraph
Dim oParChk As Paragraph
eTime = Timer
With ActiveDocument
TrkStatus = .TrackRevisions
.TrackRevisions = False
End With
With Application
SBar = .DisplayStatusBar
.DisplayStatusBar = True
.ScreenUpdating = False
End With
Set oParRef = ActiveDocument.Range.Paragraphs(1)
Set oParChk = oParRef.Next
Do
'*** Stet out first if block to delete duplicated empty paragraphs.
If Len(oParRef.Range.Text) > 1 Then
Do
'An empty last paragraph may throw an error on the last loop.
On Error GoTo Err_Exit
If oParRef.Range = oParChk.Range Then
oParChk.Range.Delete
Else
Set oParChk = oParChk.Next
End If
Loop Until oParChk Is Nothing
End If '***
Set oParRef = oParRef.Next
'Skip errors.
On Error Resume Next
Set oParChk = oParRef.Next
On Error GoTo 0
Loop Until oParRef Is Nothing
Err_Exit:
With Application
.StatusBar = False
.DisplayStatusBar = SBar
.ScreenUpdating = True
End With
ActiveDocument.TrackRevisions = TrkStatus
MsgBox "Finished. Elapsed time: " & (Timer - eTime + 86400) Mod 86400
& " seconds."
End Sub
lines of text in a document.
It used a For x = Count method to go through and check the range of
one paragraph to the the range of every other paragraph and delete any
duplicates.
It had two If ... End If blocks. The first check the para range
length. If = then the second performed a text comparison. I assume
the author thought that it would save time by doing a text comparison
only on paras of equal length.
The procedure worked as advertised, however with a longer document it
took a long time.
I created about 800 paragraphs and determined that it was actually
much quicker to bypass the the first length check and just do a range
comparison on every paragraph. Down from 200 seconds to 75 seconds!
Next I remembered an method that Jezebel showed me for stepping
through items using the .Next (property or method I am never sure
which).
I adapted the code as follows and the time taken was down to 3
seconds!
Anyway, I just wanted to share this with the group:
Sub KillDuplicateParagraphs()
Dim SBar As Boolean
Dim TrkStatus As Boolean
Dim eTime As Single
Dim oParRef As Paragraph
Dim oParChk As Paragraph
eTime = Timer
With ActiveDocument
TrkStatus = .TrackRevisions
.TrackRevisions = False
End With
With Application
SBar = .DisplayStatusBar
.DisplayStatusBar = True
.ScreenUpdating = False
End With
Set oParRef = ActiveDocument.Range.Paragraphs(1)
Set oParChk = oParRef.Next
Do
'*** Stet out first if block to delete duplicated empty paragraphs.
If Len(oParRef.Range.Text) > 1 Then
Do
'An empty last paragraph may throw an error on the last loop.
On Error GoTo Err_Exit
If oParRef.Range = oParChk.Range Then
oParChk.Range.Delete
Else
Set oParChk = oParChk.Next
End If
Loop Until oParChk Is Nothing
End If '***
Set oParRef = oParRef.Next
'Skip errors.
On Error Resume Next
Set oParChk = oParRef.Next
On Error GoTo 0
Loop Until oParRef Is Nothing
Err_Exit:
With Application
.StatusBar = False
.DisplayStatusBar = SBar
.ScreenUpdating = True
End With
ActiveDocument.TrackRevisions = TrkStatus
MsgBox "Finished. Elapsed time: " & (Timer - eTime + 86400) Mod 86400
& " seconds."
End Sub