0

我使用同事制作的宏来扫描文档并突出显示指定的亚洲字体字符(例如句号 [ChrW(65294)]、撇号 [ChrW(65287)])。它工作正常,并且完全符合我的需要(突出显示亚洲字体字符),但是因为它是拼凑在一起并随着时间的推移而添加的,所以它非常长而且不是很优雅。

我有时需要与其他同事分享它,而且不得不使用这么长的宏很麻烦。

这是代码示例(实际代码有数百行):

Sub HighlightAsianCharacters
Selection.Find.ClearFormatting
    With Selection.Find
'full stop
        .Text = ChrW(65294)
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchByte = True
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = False
        .MatchFuzzy = False
    End With
    Selection.Find.Execute
    While Selection.Find.Found
    Options.DefaultHighlightColorIndex = wdTurquoise
    Selection.range.HighlightColorIndex = wdTurquoise
    Selection.Find.Execute
    Wend
    Selection.Find.ClearFormatting
    With Selection.Find
'apostrophe
        .Text = ChrW(65287)
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchByte = True
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = False
        .MatchFuzzy = False
    End With
    Selection.Find.Execute
    While Selection.Find.Found
    Options.DefaultHighlightColorIndex = wdTurquoise
    Selection.range.HighlightColorIndex = wdTurquoise
    Selection.Find.Execute
    Wend
End Sub

有谁知道如何将字符代码放入数组中(例如),这样宏就不必是页面和页面的长度?

非常感谢您的帮助!

4

1 回答 1

0

为什么不简单地创建一个替换这些字符之一并将该字符作为参数传递的子例程。然后为您要处理的每个字符调用例程。

Sub HighlightAllAsianCharacters
    HighlightAsianCharacter ChrW(65294)
    HighlightAsianCharacter ChrW(65287)
    (...)
End Sub

Sub HighlightAsianCharacter(asianChr as string)
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = asianChr
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchByte = True
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = False
        .MatchFuzzy = False
    End With
    Selection.Find.Execute
    While Selection.Find.Found   
        Options.DefaultHighlightColorIndex = wdTurquoise
        Selection.range.HighlightColorIndex = wdTurquoise
        Selection.Find.Execute
    Wend
End Sub

当然,您可以先收集数组或集合中的所有字符,然后循环遍历它,但我真的不认为这样做有什么意义。

于 2019-07-19T06:40:29.647 回答