Link to home
Start Free TrialLog in
Avatar of FaheemAhmadGul
FaheemAhmadGulFlag for United Kingdom of Great Britain and Northern Ireland

asked on

A Word Macro to delete all those Paragraph in a Word Document that contain a given Word

I need help with writing a word macro that will go through a given word document and delete all those paragraphs in it which contain the word “apple” in them.  
Thank you for your help in anticipation.
Avatar of Jorge Sanchez
Jorge Sanchez
Flag of Ecuador image

Here's a function that will replace all the occurrences of a text with a different one:
Public Shared Sub ReplaceSingle(ByVal doc As Microsoft.Office.Interop.Word.Document, ByVal sFind As String, ByVal sReplace As String, ByVal all As Boolean)
        For Each rngRange As Range In doc.StoryRanges
            With rngRange.Find
                .ClearFormatting()
                .Replacement.ClearFormatting()
                .Text = sFind
                .Wrap = WdFindWrap.wdFindContinue
                If all Then
                    .Replacement.Text = sReplace
                    .Execute(Replace:=WdReplace.wdReplaceAll)
                Else
                    .Replacement.Text = sReplace
                    .Execute(Replace:=WdReplace.wdReplaceOne)
                End If
            End With
        Next
    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Najam Uddin
Najam Uddin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of FaheemAhmadGul

ASKER

Many thanks. I am very grateful. Regards