vba 删除word doc中所有重复文本实例的宏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4832546/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 12:35:07  来源:igfitidea点击:

Macro to delete all repeated instances of text in word doc

vbams-word

提问by MAW74656

I'm looking for a simple way to delete repeated text in a Word 2007 document. If there are some shortcuts with the Find/Replace commands, I'm not seeing it. Otherwise, can someone recommend how I might write a macro that works like the following:

我正在寻找一种简单的方法来删除 Word 2007 文档中的重复文本。如果查找/替换命令有一些快捷方式,我没有看到它。否则,有人可以推荐我如何编写一个如下所示的宏:

1- Select a block of text (could be mulitple paragraphs, have bullet points, etc).
2- Run the macro or do the command.
3- The macro or command deletes all instances of the selected text block.

1- 选择一个文本块(可以是多个段落,有项目符号等)。
2- 运行宏或执行命令。
3- 宏或命令删除所选文本块的所有实例。

Any insight here?

这里有什么见解吗?

回答by mupan

Selection.Textreturns the current selection's text.

Selection.Text返回当前选择的文本。

In principle, the syntax for your Replace command is:

原则上,您的 Replace 命令的语法是:

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
    .Text = Selection.Text
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

You replace by an empty string, thus delete every instance of the text to find.

您替换为空字符串,从而删除要查找的文本的每个实例。

But: You have to decide of course how to handle the formatting of the selected text, maybe have to analyze your selected block first because replacing might not work with control chars neglected by Selection.Text... This is just the start, you need to specify what you want and then ask again, yourself or us. Meanwhile Record macroand the word vba reference are your friends.

但是:您当然必须决定如何处理所选文本的格式,可能必须首先分析您选择的块,因为替换可能不适用于Selection.Text忽略的控制字符......这只是开始,您需要指定您想要什么,然后再次询问,您自己或我们。同时记录宏和单词 vba 参考是您的朋友。

回答by D.R.

For alpha number text (with out newlines), you can use Find/Replace however as soon as you get in the realm of bullets, new paragraphs, etc. it will no longer do exact matching. If you often need to do that, I would suggest using a program like LaTeX to write your documents. LaTeX would allow you do this style of exact matching of large blocks of text. If you're on a Windows machine, a great LaTeX package would be proTeXt which can be found at http://www.tug.org/protext/

对于字母数字文本(没有换行符),您可以使用查找/替换,但是一旦您进入项目符号、新段落等领域,它将不再进行精确匹配。如果您经常需要这样做,我建议您使用 LaTeX 之类的程序来编写文档。LaTeX 将允许您对大块文本进行这种精确匹配。如果你使用的是 Windows 机器,一个很棒的 LaTeX 包是 proTeXt,可以在http://www.tug.org/protext/找到

回答by A.P.

I had exactly the same problem, huge mailing list and I just needed the emails. I solved it by copying the text in excel, filtering sentence by sentence (or bullets) the paragraph i wanted to remove and deleting all rows. Mine was more than 270 pages, worked just fine (as long as the text is not too long, it was a lot faster than replaceing sentence by sentence in word.) Or if it is an option, just filter with a text filter - begins with "to:" , then you are done in 10 secs. Hope it helps.

我有完全相同的问题,巨大的邮件列表,我只需要电子邮件。我通过复制excel中的文本,逐句(或项目符号)过滤我想要删除的段落并删除所有行来解决它。我的有 270 多页,效果很好(只要文本不是太长,这比在单词中逐句替换要快得多。)或者如果它是一个选项,只需使用文本过滤器进行过滤 - 开始使用 "to:" ,那么您将在 10 秒内完成。希望能帮助到你。

回答by user2948764

Just Press Ctrl+H to select and replace text, Type the text you want to remove and Press Replace All leaving replace with field BLANK

只需按 Ctrl+H 选择和替换文本,键入要删除的文本,然后按全部替换,将替换为空白字段