PDA

View Full Version : Kill Duplicate Paragraphs



VB-AN-IZ
11-01-2015, 02:43 PM
Assembling wordlists from various sources invariably results in identical paragraphs. I've found a macro that deletes all of the repeated paragraphs, listing one version of each repeated paragraph, but now I need one that deletes the repeated paragraphs as well as the original. So the existing one will turn this:

A
A
A
B
C
C
D
E
E
E

..into:

A
B
C
D
E

But I need one that will only keep the paragraphs that aren't repeated:

B
D

How would I modify the existing macro so it removes all paragraphs that are duplicated?

Thanks!

Dim AmountMoved As Long
Dim myRange As Range
Set myRange = ActiveDocument.Paragraphs(1).Range
AmountMoved = myRange.MoveEnd(Unit:=wdParagraph, Count:=1)
Do While AmountMoved > 0
If myRange.Paragraphs(1).Range.Text = _
myRange.Paragraphs(2).Range.Text Then
myRange.Paragraphs(2).Range.Delete
AmountMoved = myRange.MoveEnd(Unit:=wdParagraph, Count:=1)
Else
AmountMoved = myRange.MoveEnd(Unit:=wdParagraph, Count:=1)
myRange.MoveStart Unit:=wdParagraph, Count:=1
End If
Loop
End Sub

gmaxey
11-02-2015, 02:10 PM
ub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim arrDups() As String
Dim oCol As New Collection, oColDump As New Collection
Dim lngIndex As Long, lngDupe As Long
Dim oPar As Paragraph
For Each oPar In ActiveDocument.Paragraphs
On Error Resume Next
oCol.Add oPar.Range.Text, oPar.Range.Text
If Err.Number <> 0 Then
oColDump.Add oPar.Range.Text, oPar.Range.Text
End If
Next oPar
For lngIndex = oCol.Count To 1 Step -1
For lngDupe = 1 To oColDump.Count
If oCol.Item(lngIndex) = oColDump(lngDupe) Then
oCol.Remove (lngIndex)
Exit For
End If
Next lngDupe
Next lngIndex
ActiveDocument.Range.Delete
For lngIndex = 1 To oCol.Count
ActiveDocument.Range.InsertAfter oCol.Item(lngIndex)
Next lngIndex
lbl_Exit:
Exit Sub

End Sub

VB-AN-IZ
11-04-2015, 10:32 PM
Exactly what I was hoping for. Thanks so much for your help, I really appreciate it!

gmaxey
11-05-2015, 03:26 AM
You're welcome. Glad to help.