vba 如何使用宏查找 MS Word 文档中的所有首字母缩略词?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10917155/
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
How Do I Find All Acronyms in an MS Word Document Using a Macro?
提问by MAbraham1
I have a document with many acronyms that need to be captured and put into an acronyms table at the end of the document.
我有一个包含许多首字母缩略词的文档,需要捕获并放入文档末尾的首字母缩略词表中。
The term acronym has various meanings. I'd like to create a table that has all of the words that are initialized; two or more capitalized letters that are short for a longer meaning. I.e., CD-ROM, USB, SYNC, MMR, ASCAP, etc.
术语首字母缩略词有多种含义。我想创建一个包含所有已初始化单词的表;两个或多个大写字母的缩写,表示较长的含义。即 CD-ROM、USB、SYNC、MMR、ASCAP 等。
How do I create a macro to do this?
我如何创建一个宏来做到这一点?
回答by Tim Williams
Something like this might get you started. Add a reference to "Microsoft VBScript Regular Expressions" (Edit Macro: Tools > References). This library is the file, "vbscript.dll".
像这样的事情可能会让你开始。添加对“Microsoft VBScript 正则表达式”的引用(编辑宏:工具 > 引用)。这个库是文件“vbscript.dll”。
You may need to adjust the regexp if all your acronyms aren't only upper-case letters (eg some may contain numbers).
如果您的所有首字母缩略词都不是大写字母(例如,有些可能包含数字),您可能需要调整正则表达式。
Sub Acronyms()
Dim dict, k, tmp
Dim regEx, Match, Matches
Dim rngRange As Range
Set regEx = New RegExp
Set dict = CreateObject("scripting.dictionary")
regEx.Pattern = "[A-Z]{2,}" '2 or more upper-case letters
regEx.IgnoreCase = False
regEx.Global = True
Set Matches = regEx.Execute(ActiveDocument.Range.Text)
For Each Match In Matches
tmp = Match.Value
If Not dict.Exists(tmp) Then dict.Add tmp, 0
dict(tmp) = dict(tmp) + 1
Next
For Each k In dict.Keys
Debug.Print k, dict(k)
Next k
End Sub
回答by Vignesh
Thanks Tim, your code works great!
谢谢蒂姆,你的代码很好用!
If it will be of any use to others, the pattern [A-Z]{1,}([a-z]*|\&|\.*)[A-Z]{1,}
will find more acronyms...
如果它对其他人有任何用处,则该模式[A-Z]{1,}([a-z]*|\&|\.*)[A-Z]{1,}
将找到更多首字母缩略词...
(I do not have permission to post comments, hence adding this as answer)
(我无权发表评论,因此将其添加为答案)
Edit (still no way to add comments): \b[A-Z]{1,}([a-z*]|\&|\.|\-)[A-Z]{1,}\b
is more robust, but will fail if the last character of the acronym is not capitalized.
编辑(仍然无法添加评论):\b[A-Z]{1,}([a-z*]|\&|\.|\-)[A-Z]{1,}\b
更强大,但如果首字母缩略词的最后一个字符未大写,则会失败。
回答by Adam
I have found the following works well (where some business name acronyms are tolerable). I use this to test data entries in Access, it should also work for a Word document range.
我发现以下效果很好(可以容忍某些商业名称首字母缩写词)。我用它来测试 Access 中的数据条目,它也应该适用于 Word 文档范围。
objRegExp.Pattern = "([A-Z]{1,}((\&(?![A-Z]\s[\w]{3})\w*)+|\.\w*)+)|[A-Z]{2,}(?![A-Z]*\s[A-Z]{1}[a-z])"
- J&K =Match
- JK&S =Match
- J.S.S =Match
- JK&S.K =Match
- JSK =Match
- JK =Match
- DKD And Sons =No Match
- J&K Engineering =No Match
- PKF Rogers and Associates =No Match
- J&K =匹配
- JK&S =匹配
- JSS =匹配
- JK&S.K =匹配
- JSK =匹配
- JK =匹配
- DKD 和儿子 = 不匹配
- J&K 工程 = 不匹配
- PKF Rogers and Associates = 不匹配
I use RegExHeroto test my expressions
我使用RegExHero来测试我的表达
回答by Sina
I used the following to find abbreviations in my PhD thesis. They were all in "()".
我使用以下内容在我的博士论文中查找缩写。它们都在“()”中。
regEx.Pattern = "\([A-Z]{1,}([a-z]*|\&|\.|\-*)[A-Z]{1,}\)"
回答by MAbraham1
You will be running a macro on the main Word document. Open a separate Word document that is blank. This will be used to store discovered the acronyms.
您将在主 Word 文档上运行宏。打开一个单独的空白 Word 文档。这将用于存储发现的首字母缩略词。
- Press "Record Macro". Choose a unique name, and assign a shortcut key such as CTRL + ALT + A.
- Open the Find dialogue (CTRL + F). Paste the following search text:
<[A-Z]{2,}>.
In the Find dialogue, choose "More" > check the box for "Use Wildcards". Click the Find Next button. - Right-click on the selected text, being careful not to change the highlight. Select Copy from the context menu.
- Navigate to the separate Word document (ALT + TAB, select the Word document). Paste the copied text, and hit Enter. ALT + TAB back to the original Word document.
- Close the find dialogue and click the right arrow once. This moves the cursor off the highlighted text, and readies it for the next search.
- Stop the macro recording.
- 按“录制宏”。选择一个唯一的名称,并指定一个快捷键,例如 CTRL + ALT + A。
- 打开查找对话框 (CTRL + F)。粘贴以下搜索文本:
<[A-Z]{2,}>.
在“查找”对话框中,选择“更多”> 选中“使用通配符”框。单击查找下一个按钮。 - 右键单击所选文本,注意不要更改突出显示。从上下文菜单中选择复制。
- 导航到单独的 Word 文档(ALT + TAB,选择 Word 文档)。粘贴复制的文本,然后按 Enter。ALT + TAB 回到原来的 Word 文档。
- 关闭查找对话框并单击右箭头一次。这会将光标移出突出显示的文本,并为下一次搜索做好准备。
- 停止宏录制。
You now have a macro that finds a word containing two or more capitalized letters, and saves the text to a separate document. In order to search for the remaining acronyms, press CTRL + ALT + A continuously until the end of the document has been reached. Or, edit the macro, and add while a loop.
您现在有一个宏,它可以查找包含两个或更多大写字母的单词,并将文本保存到单独的文档中。要搜索剩余的首字母缩略词,请连续按 CTRL + ALT + A,直到到达文档末尾。或者,编辑宏,并添加 while 循环。
Here is what the macro looks like (without the loop):
这是宏的样子(没有循环):
Sub GetAcronyms()
Selection.Find.ClearFormatting
With Selection.Find
.Text = "<[A-Z]{2,}>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute
Selection.Copy
Windows("Document1.docx").Activate
Selection.PasteAndFormat (wdPasteDefault)
Selection.TypeParagraph
Windows("TheOriginalDocument.docx").Activate
Selection.MoveRight Unit:=wdCharacter, Count:=1
End Sub