VBA Word - 突出显示所有非基本拉丁字符

时间:2015-06-23 04:15:03

标签: arrays vba word-vba highlight latin1

我正在尝试编写一个代码,该代码将突出显示Word文档中的所有非基本拉丁字符(例如,所有非字母数字的字符,以及某些字符,如“。”,“(”,“)” ,“?”,“/”等。)

这段代码有效,但对于我想要实现的目标而言需要太长时间。它的工作原理是突出显示整个文档,然后从数组中找到的“好”字符中删除突出显示,使非基本拉丁字符突出显示。

Sub HighlightNonStandardCharacters()

  Application.ScreenUpdating = False

Dim MyRange As Range
Dim i As Long
Dim NormalCharacters

NormalCharacters = Array(" ", "!", Chr(34), "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?", "@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "[", "\", "]", "^", "_", "`", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~", Chr(13), "¶")

' highlights all document text 
ActiveDocument.Range.HighlightColorIndex = wdTurquoise

For i = 0 To UBound(NormalCharacters)
Set MyRange = ActiveDocument.Range
With MyRange.Find
.Text = NormalCharacters(i)
.Format = True
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False

Do While .Execute(Forward:=True) = True
' removes highlighting from the “standard” characters leaving “non-standard” characters highlighted 
MyRange.HighlightColorIndex = wdAuto
Loop
End With
Next
  Application.ScreenUpdating = True

End Sub

我想知道是否有更快的方法来突出显示不在数组中的所有字符或其他方式来实现目标。

任何想法都会受到赞赏。

1 个答案:

答案 0 :(得分:1)

谢谢KazimierzJawor

这正是我所需要的。更快更简单。我没有意识到在搜索搜索中使用通配符有多强大。

这就是我提出的:

Sub HighlightNonBasicLatinCharacters()
Options.DefaultHighlightColorIndex = wdTurquoise
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Highlight = True
    With Selection.Find
    .Text = "[! -~¶]"
    .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