Solved

Macro To Remove Duplicate Paragraphs from a Word Document

Posted on 2008-10-11
3
2,066 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 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
 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

This article will show, step by step, how to integrate R code into a R Sweave document
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

624 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