Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Macro To Remove Duplicate Paragraphs from a Word Document

Posted on 2008-10-11
3
Medium Priority
?
2,153 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

721 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