托比亚斯的回答似乎就是门票。只是想补充一点,我刚刚意识到量词在字符类中毫无意义。还注意到同事的电子邮件通常在数字前面和美元符号后面有一个空格,因此下面是一些更好的正则表达式(对于美元金额):
RegExp.Pattern = "\$\s*([\,\d]*(?:\.\d{2})?)"
从中得到一些启发:超链接 range.start 和 range.end 指的是什么? https://stackoverflow.com/questions/58390508/what-does-a-hyperlink-range-start-and-range-end-refer-to想出了这个:
Sub trueUpAttempt()
Dim OrigLength As Long
Debug.Print ActiveDocument.Characters.Count
Dim SelStart As Long
Dim SelEnd As Long
Dim SelLength As Long
Dim rHyperlink As Range
Dim wdHyperlink As Hyperlink
For Each wdHyperlink In ActiveDocument.Hyperlinks
Set rHyperlink = wdHyperlink.Range
'Debug.Print rHyperlink.Start
'Debug.Print rHyperlink.End
'Debug.Print rHyperlink.End - rHyperlink.Start
Debug.Print rHyperlink.End - rHyperlink.Start - Len(rHyperlink)
'there's got to be some way to true up the character offset, even if its ugly
Debug.Print ActiveDocument.Characters.Count + rHyperlink.End - rHyperlink.Start - Len(rHyperlink)
Next
End Sub
这不是一个解决方案,但我认为这是协调字符偏移的轮廓。这都是因为单词正在计算例如中的所有 62 个字符{HYPERLINK "http://www.smithany.com"} http://www.smithany.com
Edit2023 年 7 月 22 日尝试相反的托拜斯建议:
Sub DollarHighlighter2()
Set regExp = New regExp
Dim objMatch As Match
Dim colMatches As MatchCollection
Dim offsetEnd As Long
offsetEnd = Selection.End
regExp.Pattern = "\$([\,\d{1,3}]*(?:\.\d{2})?)"
regExp.Global = True
Set allMatches = regExp.Execute(Selection.text) ' Execute search.
For i = allMatches.Count - 1 To 0 Step -1
'MsgBox allMatches.Item(i)
ActiveDocument.Range(offsetEnd - allMatches.Item(i).FirstIndex, End:=offsetEnd - allMatches.Item(i).FirstIndex + allMatches.Item(i).Length).FormattedText.HighlightColorIndex = wdYellow
Next
End Sub
但这似乎仍然对链接有类似的问题,也许还有其他内容。我也尝试了相同的向前确定范围,但反向循环匹配并遇到了类似的问题。
此处示例文件的工作链接(无 ssl):http://www.smithany.com/exampleDollarHighliter.docx http://www.smithany.com/exampleDollarHighliter.docx
原来的:我看过其他几篇 StackOverflow 帖子,例如这篇文章:如何在word中使用VBA(宏)使用/启用(RegExp对象)正则表达式 https://stackoverflow.com/questions/25102372/how-to-use-enable-regexp-object-regular-expression-using-vba-macro-in-word有关使用 Microsoft VB 脚本正则表达式 5.5 参考在 Microsoft Word 中通过 VBA 使用正则表达式的信息。
这帮助我准备了以下内容,我在 Word 中使用它来突出显示美元金额:
Sub dollarHighlighter()
Set regExp = New regExp
Dim objMatch As Match
Dim colMatches As MatchCollection
Dim offsetStart As Long
offsetStart = Selection.Start
regExp.Pattern = "\$([\,\d{1,3}]*(?:\.\d{2})?)"
regExp.Global = True
Set colMatches = regExp.Execute(Selection.Text) ' Execute search.
For Each objMatch In colMatches ' Iterate Matches collection.
Set myRange = ActiveDocument.Range(objMatch.FirstIndex + offsetStart,
End:=offsetStart + objMatch.FirstIndex + objMatch.Length)
myRange.FormattedText.HighlightColorIndex = wdYellow
Next
End Sub
虽然这在文本中的美元金额列表上按预期工作(在大多数情况下 -正则表达式的缺陷之一是故意有点松散)当 Word 文档中存在超链接时,它不会按预期工作。
在这种情况下,突出显示的字符的偏移量似乎以某种不可预测的方式发生了变化。我认为这是因为 document.xml 源文件中有很多新的 xml/css 。
最终,我的首要问题是,我可以使用正则表达式来突出显示 Word 文档内容,即使它包含超链接吗?这是一个偏移问题还是我应该在压缩的 xml 上运行正则表达式,重新压缩并重新打开以获得更好的结果?当我在源代码上测试各种正则表达式变体时,我得到了预期的结果,但在格式化 Word 范围时却没有得到预期的结果。
我也在这里问过这个问题:但意识到这是一个古老的帖子......
对于下面的问题,这里有一些可能有用的链接:
示例文档http://www.smithany.com/test.docx http://www.smithany.com/test.docx
step 1
http://www.smithany.com/wordusd1.jpg http://www.smithany.com/wordusd1.jpg
Step 2
http://www.smithany.com/wordhighlighterrun.jpg http://www.smithany.com/wordhighlighterrun.jpg以及发生了什么http://www.smithany.com/whatactuallyhappens.jpg http://www.smithany.com/whatactuallyhappens.jpg
临时解决方法:如下所示,如果不堆叠循环,Word 的通配符查找速度会很快。尝试这个:
Sub Macro2()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.highlight = True
With Selection.Find
.Text = "$[0-9,]{1,}"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.highlight = True
With Selection.Find
.Text = "$[0-9,]{1,}.[0-9]{2,3}"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
这基本上突出显示了所有美元金额。也就是说,像匹配各种日期格式这样的复杂表达式可能会变得混乱,但我认为一次一步地执行它们是可能的。