Solved

Macro To Remove Duplicate Paragraphs from a Word Document

Posted on 2008-10-11
3
1,948 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 500 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
 

Author Closing Comment

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

Author Comment

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

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

789 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