Word VBA - 在分隔符之间查找文本并转换为小写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1512415/
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
Word VBA - Find text between delimiters and convert to lower case
提问by JohnZaj
I would like to find text which is between the < and > characters, and then turn any found text into "normal" case, where first letter of word is capitalized. Here is what I have thus far:
我想找到 < 和 > 字符之间的文本,然后将任何找到的文本转换为“正常”情况,其中单词的第一个字母大写。这是我迄今为止所拥有的:
Function findTextBetweenCarots() As String
Dim strText As String
With Selection
.Find.Text = "<" ' what about <[^0-9]+> ?
.Find.Forward = True
.Find.Wrap = wdFindContinue
End With
Selection.Find.Execute
' Application.Selection. ' how do I get the text between the other ">"?
findCarotSymb = Application.Selection.Text
End Function
Or, is there a better way of doing this? I also approached the problem using the VBScript Regex 5.5 library, which worked on simple documents, but not on certain documents with complex tables. For example, trying to just bold the text (for simplicity):
或者,有没有更好的方法来做到这一点?我还使用 VBScript Regex 5.5 库解决了这个问题,该库适用于简单文档,但不适用于具有复杂表格的某些文档。例如,尝试将文本加粗(为简单起见):
Sub BoldUpperCaseWords()
Dim regEx, Match, Matches
Dim rngRange As Range
Set regEx = New RegExp
regEx.Pattern = "<[^0-9]+>"
regEx.IgnoreCase = False
regEx.Global = True
Set Matches = regEx.Execute(ActiveDocument.Range.Text)
For Each Match In Matches
ActiveDocument.Range(Match.FirstIndex, Match.FirstIndex + Len(Match.Value)).Bold = True
Next
End Sub
would not work in a document with tables. In fact, it would not even bold the correct text (the text between the <>
. This leads me to believe I have a broader issue here that I am missing.
在带有表格的文档中不起作用。事实上,它甚至不会加粗正确的文本(<>
.之间的文本。这让我相信我在这里有一个更广泛的问题,我错过了。
采纳答案by Nick Dandoulakis
I can not test your code but try this regex
我无法测试你的代码,但试试这个正则表达式
regEx.Pattern = "<[^0-9<>]+>"
Your regex would match in "<foo><bar>"
the whole string.
The above regex will match only the <foo>
, then the <bar>
and so on.
您的正则表达式将匹配"<foo><bar>"
整个字符串。
上面的正则表达式将只匹配<foo>
,然后<bar>
等等。