PDA

View Full Version : [SOLVED:] Help - Comparing specific paragraphs in a document for duplications



Scraps
09-14-2015, 06:21 AM
Apologies in advance for what is perhaps (I hope) is an easy solution - I've done a fair bit of simple work with VBA but I'm a bit stuck with this one.

How do i create a macro that can check specific paragraphs in a text document for duplications, then output a message or similar so that any instances can be manually located.

I don't wish to check every single paragraph in the document as there will be lots of duplicates that are not relevant - just paragraphs that (fortunately) follow on immediately after a non-variable line of text.
eg

<PNRS>
This paragraph is going to be a duplicate

....

<PNRS>
This paragraph is unique
....
<PNRS>
This paragraph is going to be a duplicate

etc

Could anyone please help or point me in the right direction. Thanks!

PS Word 2010/2013

Scraps
09-16-2015, 04:57 AM
Anyone?

I figure I probably need to do something similar to the following:-

Set X as every paragraph following text "<PNRS>", then for each X till end, compare with each subsequent X, print paragraph (or part of) to outbox. But I'm unsure on how to do it in VBA for Word.

Any help really appreciated.

Scraps
09-21-2015, 07:10 AM
Still struggling with this one.

I know how to run through the document and stop at the sentence after each occurrence of "<PNRS>", and separately I know I could run through every paragraph. But putting it together so that it stops at only every paragraph after the "<PNRS>" and then compares them all I'm still stuck with.

gmayor
09-21-2015, 10:14 PM
The following should work. It highlights any paragraph following <PNRS> that duplicates an earlier paragraph that follows <PNRS>

Sub FindDuplicates()
Dim oRng As Range
Dim oFind As Range
Set oRng = ActiveDocument.Range
With oRng.Find
Do While .Execute(FindText:="<PNRS>")
Set oFind = oRng.Paragraphs(1).Range.Next.Paragraphs(1).Range
With oFind.Find
'MsgBox oFind.Text
Do While .Execute(FindText:=oFind.Text)
If InStr(1, oFind.Paragraphs(1).Range.Previous.Paragraphs(1).Range.Text, "<PNRS>") > 0 Then
oFind.HighlightColorIndex = 16
oFind.Collapse 0
End If
Loop
End With
oRng.Collapse 0
Loop
End With
lbl_Exit:
Set oRng = Nothing
Set oFind = Nothing
Exit Sub
End Sub

Scraps
09-22-2015, 08:09 AM
Brilliant! Thanks a lot for your help Graham, that works perfectly. :). Really appreciated.

I've moved the MsgBox oFind.Text line down into the If...End If statement so that it produces a visible output of any such duplicates (since in my case the files are bulky, but the duplicates are rare and often non-existent). Will mark as solved.

Ta,

Lee