vba MS Word 宏 - 删除段落
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/829947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MS Word Macro - Delete Paragraphs
提问by Paul Ellery
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 一无所知,但刚刚收到了一个巨大而笨拙的文档,我需要快速编辑。
回答by Paul Ellery
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.
将 'search' 的值设置为您要查找的文本。它非常粗糙,如果您的文本没有出现在其中的某处,它将删除该段落。
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 上试过这个。有点吓人,但似乎有效!