正则表达式与超链接ActiveDocument.Range和格式

时间:2018-05-23 03:04:07

标签: regex vba ms-word

我已经看过其他几个StackOverflow帖子,比如这个: How to Use/Enable (RegExp object) Regular Expression using VBA (MACRO) in word 使用Microsoft VB脚本正则表达式5.5参考。使用VBA在Microsoft Word中使用正则表达式。

这帮助我准备了以下内容,我在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范围时却没有。

我在这里也问了这个问题:https://social.msdn.microsoft.com/Forums/en-US/3a95c5e4-9e0c-4da9-970f-e0bf801c3170/macro-for-a-regexp-search-replace?forum=isvvba&prof=required但是意识到这是一个古老的帖子......

下面的问题,这里有一些可能有用的链接: 一个示例文档 http://www.smithany.com/test.docx 步骤1 http://www.smithany.com/wordusd1.jpg 第2步 http://www.smithany.com/wordhighlighterrun.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

基本上可以突出显示所有美元金额。也就是说,匹配各种日期格式的复杂表达式可能会变得混乱,但我想这一切都可以一步一步地完成它们。

1 个答案:

答案 0 :(得分:3)

我多年没碰过过VBA,但我觉得这就像骑自行车一样。

无论如何,这是一个应该帮助你的子。它基于Cindy Meister的声音推荐,并使用可选部分的匹配模式集合填补Regex和Wildcard Find之间的空白。

首先,通配符匹配:$[0-9,]{1,}$[0-9,]{1,}.[0-9]{2}

毕竟,它并没有那么不同,不是吗?但是,要考虑可选的小数部分,我必须使用两种模式。

以下是常规:

Sub WildcardsHighlightWords()
    Dim Word As Range
    Dim WildcardCollection(2) As String
    Dim Words As Variant
    WildcardCollection(0) = "$[0-9,]{1,}"
    WildcardCollection(1) = "$[0-9,]{1,}.[0-9]{2}"
    Options.DefaultHighlightColorIndex = wdYellow
    'Clear existing formatting and settings in Find
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    'Set highlight to replace setting.
    Selection.Find.Replacement.Highlight = True
    'Cycle through document and find wildcards patterns, highlight words when found
    For Each Word In ActiveDocument.Words
        For Each WildcardsPattern In WildcardCollection
            With Selection.Find
                .Text = WildcardsPattern
                .Replacement.Text = ""
                .Forward = True
                .Wrap = wdFindContinue
                .Format = True
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = True
                .MatchSoundsLike = False
                .MatchAllWordForms = False
            End With
            Selection.Find.Execute Replace:=wdReplaceAll
        Next
    Next
End Sub

如果需要,应该很容易扩展或修改这种方法。

这最终使得美元数量符合预期:

enter image description here

注意:量词{n , m}中的分隔符在所有本地化中都不相同,例如它是德语版本中的{n ; m}。