tags:

views:

765

answers:

1

Could somebody please help me with a MS Word Macro that would search for a specific symbol in every paragraph throughout the document and delete paragraphs that DO NOT contain that symbol.

I know practically nothing about VBA, but just received a huge & unwieldy document I need to edit real fast.

+2  A: 

Here's a quick macro that should do what you want - use with caution, and don't forget to backup!

Set the value of 'search' to be the text that you're looking for. It's very crude, and will delete the paragraph if your text does not appear somewhere within it.

Sub DeleteParagraphContainingString()

    Dim search As String
    search = "delete me"

    Dim para As Paragraph
    For Each para In ActiveDocument.Paragraphs

        Dim txt As String
        txt = para.Range.Text

        If Not InStr(LCase(txt), search) Then
            para.Range.Delete
        End If

    Next

End Sub

I've tried this on Office 2007. Bit scary, but seems to work!

tequila2k
The request is to delete paragraphs NOT containing the string (or symbol). Change the "If InStr(..." to "If Not InStr(...".
Ken Paul
tequila2k, Ken Paul, thanks a lot for your help. Tried the macro just now, and it did the trick. Saved me hours of tedious manual editing. Thanks again.
Ah yes, thanks Ken - I've now corrected the answer.
tequila2k