MS Word 宏 - 删除段落

发布于 2024-07-19 04:56:55 字数 115 浏览 2 评论 0原文

有人可以帮我使用 MS Word 宏吗?它可以在整个文档的每个段落中搜索特定符号,并删除不包含该符号的段落。

我对VBA几乎一无所知,但刚刚收到一个巨大的& 笨重的文档我需要快速编辑。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

软的没边 2024-07-26 04:56:55

这是一个快速宏,应该可以完成您想要的操作 - 谨慎使用,并且不要忘记备份!

将“搜索”的值设置为您要查找的文本。 它非常粗糙,如果您的文本没有出现在该段落中的某个位置,它将删除该段落。

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

我在 Office 2007 上尝试过这个。有点吓人,但似乎有效!

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!

小…红帽 2024-07-26 04:56:55

保罗的回答是一个好的开始,但我认为已经过时了。 我和OP有同样的问题,但必须修改Paul的答案才能在Word 2016中工作。请记住,如果InStr返回0那么这意味着它找不到匹配,如果它返回> 0那么这意味着它确实找到了匹配项。 因此,如果您想翻转代码以仅删除找到的匹配项,请将“=”更改为“>”。
我希望这对未来的读者有所帮助。
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

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. 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!

   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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文