MS Word Macro - Delete Paragraphs

廉价感情. 提交于 2019-12-01 09:27:38

问题


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.


回答1:


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!



来源:https://stackoverflow.com/questions/829947/ms-word-macro-delete-paragraphs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!