Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Macro To Remove Duplicate Paragraphs from a Word Document

Posted on 2008-10-11
3
Medium Priority
?
2,201 Views
Last Modified: 2012-08-13
I have word documents that (due to some reason) end up having duplicate (or even triplicate) entries of some paragraphs. For example paragraph 1, 2 and 3 may be hundred percent identical. Similarly paragraph 4 and 5 may be identical (so forth and so on).
I would like to have a macro that will (starting from the top of the document) will compare each of the two consecutive paragraphs in the document and will delete one of the two paragraphs if it finds that those two paragraphs are identical. For example it will first compare Para 1 and 2 and if it finds that they are identical it will delete Para 1. It will then compare Para 2 (which would, after deletion of Para 1, would have now become Para 1) with Para 3 and will delete Para 2 if it finds that Para 2 is identical to Para 3. It will then compare Para 3 and Para 4 and so forth and so on. The end result of this will be that all duplicate or triplicate entries of identical Paragraphs would have been removed by the Macro.
To make my Problem easier to understand I attach a file (named File with Duplicate Entries) with duplicate entries on which I would like to run my planned macro. I also attach another file (named File without Duplicate Entries) which is how I would expect my first File (with Duplicate Entries) to look like after running the planned Macro.  
Thank you for your help in anticipation
File-With-Duplicate-Entries.doc
File-Without-Duplicate-Entries.doc
0
Comment
Question by:FaheemAhmadGul
  • 2
3 Comments
 
LVL 23

Accepted Solution

by:
irudyk earned 2000 total points
ID: 22696180
In your sample files I noticed that you wanted the last Patient 4 record kept even though the text for that paragraph had the word Anaemia rather than Anemia (as listed in the previous 2 paragraphs).  Also this paragraph had a Shift+Enter followed by an Enter (whereas the previous 2 paragraphs did not).
As such I presumed the Anaemia was a typo.  For the extra soft-return I remove these when comparing the paragraph text via the following Word VBA code which should do what you are looking for.

Sub RemoveDuplicateParagraphs()
 
Dim pCount As Long
Dim p As Long
 
pCount = ActiveDocument.Paragraphs.Count
 
For p = 1 To pCount
    If p = pCount Then Exit Sub
    If Replace(ActiveDocument.Paragraphs(p).Range.Text, Chr(11), "") = Replace(ActiveDocument.Paragraphs(p + 1).Range.Text, Chr(11), "") Then
        ActiveDocument.Paragraphs(p).Range.Delete
        p = p - 1
        pCount = pCount - 1
    End If
Next p
 
End Sub

Open in new window

0
 
LVL 1

Author Closing Comment

by:FaheemAhmadGul
ID: 31505346
Brilliant!  This worked perfectly. I am extremely grateful. Regards - Faheem
0
 
LVL 1

Author Comment

by:FaheemAhmadGul
ID: 22696800
Brilliant!  This worked perfectly. I am extremely grateful. Regards - Faheem
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question