简体   繁体   English

MS Word 宏 - 删除段落

[英]MS Word Macro - Delete Paragraphs

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.有人可以帮助我使用 MS Word 宏,它会在整个文档的每个段落中搜索特定符号,并删除不包含该符号的段落。

I know practically nothing about VBA, but just received a huge & unwieldy document I need to edit real fast.我对 VBA 几乎一无所知,但刚收到一份庞大而笨重的文档,我需要快速编辑。

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!我在 Office 2007 上试过这个。有点可怕,但似乎有效!

Paul's answer is a good start, but I think out of date.保罗的回答是一个好的开始,但我认为已经过时了。 I had the same question as the OP, but had to modify Paul's answer to work in Word 2016. Keep in mind, If InStr returns 0 then it means that it couldn't find a match, if it returns >0 then it means it did find a match.我有与 OP 相同的问题,但必须修改 Paul 的答案才能在 Word 2016 中工作。请记住,如果 InStr 返回 0,则表示找不到匹配项,如果返回 >0,则表示它确实找到了匹配项。 So if you want to flip the code to delete only found matches, change the '=' to a '>'.因此,如果您想翻转代码以仅删除找到的匹配项,请将“=”更改为“>”。 I hope this helps future readers.我希望这对未来的读者有所帮助。 PS: This code is amazing for helping clean up auto-transcripts from Zoom calls! PS:这段代码非常棒,可以帮助清理 Zoom 通话中的自动转录内容!

   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 InStr(LCase(txt), LCase(search)) = 0 Then
        para.Range.Delete
    End If

Next

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM