在单词/VBA中查找包含多个大写字母的单词 [英] Find words with more than one capital letter in word/VBA
问题描述
我有一段VBA代码使用查找查找文档中的所有首字母缩写词.为此,它使用...搜索所有由2个或多个字符组成的大写字母组成的所有单词.
I have a piece of VBA code that uses Find to find all the acronyms in a document. It does this by searching for all words consisting of capital letters that are 2 or more characters long using...
<[A-Z]{2,}>
此问题是它没有吸收所有缩写,例如CoP,W3C,DVD和CD-ROM.它在两个部分中选择了带连字符的首字母缩写词,这是不理想的,但由于用户检查了列表,因此是允许的.我也可以通过不使用...来搜索以"s"或其他字符结尾的单词的末尾.
The problem with this is it doesn't pick up all the acronyms, such as CoP, W3C, DVDs and CD-ROM. It picks up hyphenated acronyms in two parts which are not ideal but allowable as the list is checked by a user. I can also pick up words that end with an "s" or other characters by not searching to the end of the word using...
<[A-Z]{2,}
但是,这不会将任何非大写字符视为其找到的单词的一部分.
But this doesn't count any non-upper case character as part of the word it finds.
是否存在一个表达式,可以让我在任何位置搜索包含两个或更多个大写字母的单词并找到整个单词?
Is there an expression that would allow me to search for words with two or more capital letters in any location and find the whole word?
推荐答案
除非结合宏代码,否则我认为不可能在任何位置搜索包含两个或多个大写字母的单词并找到整个单词" .既然您使用的是宏,那么以下示例文本对我来说是一种有效的方法
I don't think it's possible to 'search for words with two or more capital letters in any location and find the whole word' except in combination with macro code. Since you're using a macro, anyway, here's an approach that worked for me using the following sample text
CoP, this That and AnoTher thing W3C, DVDs and CD-ROM
和此通配符组合(请注意,在Windows配置中,列表分隔符为;
,对于其他区域,可能需要,
.)
and this wildcard combination (note that the list separator in my Windows configuration is ;
, for other regions a ,
may be required).
<[A-Z][0-9A-Z\-a-z]{1;10}>
以下函数检查找到"范围内的第二个字母或以后的字母是否为大写,并向调用过程返回一个布尔值.它循环遍历给定Range
中的字符,并检查ASCII值.一旦找到一个,循环就会退出.
The following function checks whether the second or any later letter in the "found" range is capitalized and returns a boolean to the calling procedure. It loops through the characters in the given Range
, checking the ASCII value. As soon as one is found, the loop exits.
Function ContainsMoreThanOneUpperCase(rng As Word.Range) As Boolean
Dim nrChars As Long, i As Long
Dim char As String
Dim HasUpperCase
HasUpperCase = False
nrChars = rng.Characters.Count
For i = 2 To nrChars
char = rng.Characters(i).text
If Asc(char) >= 65 And Asc(char) <= 90 Then
'It's an uppercase letter
HasUpperCase = True
Exit For
End If
Next
ContainsMoreThanOneUpperCase = HasUpperCase
End Function
使用示例:
Sub FindAcronyms()
Dim rngFind As Word.Range
Dim bFound As Boolean
Set rngFind = ActiveDocument.content
With rngFind.Find
.text = "<[A-Z][0-9A-Z\-a-z]{1;10}>"
.MatchWildcards = True
.Forward = True
.wrap = wdFindStop
bFound = .Execute
Do While bFound
If bFound And ContainsMoreThanOneUpperCase(rngFind) Then
Debug.Print rngFind.text
rngFind.HighlightColorIndex = wdBrightGreen
End If
rngFind.Collapse wdCollapseEnd
bFound = .Execute
Loop
End With
End Sub
这篇关于在单词/VBA中查找包含多个大写字母的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!