如何使用 VBA 搜索并突出显示 "ChrW(n)" 亚洲字体字符数组而不是将它们列出来
How to search for and highlight an array of "ChrW(n)" Asian font characters using VBA rather than listing them out
我使用同事制作的宏来扫描文档并突出显示指定的亚洲字体字符(例如句号 [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
有谁知道如何将字符代码放入一个数组(例如),这样宏的长度就不必是一页又一页了?
非常感谢您的帮助!
为什么不简单地创建一个子例程来替换其中一个字符并将该字符作为参数传递。然后为每个要处理的字符调用例程。
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
当然,您可以先将所有字符收集到一个数组或集合中,然后对其进行循环,但我认为这样做没有任何意义。
我使用同事制作的宏来扫描文档并突出显示指定的亚洲字体字符(例如句号 [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
有谁知道如何将字符代码放入一个数组(例如),这样宏的长度就不必是一页又一页了?
非常感谢您的帮助!
为什么不简单地创建一个子例程来替换其中一个字符并将该字符作为参数传递。然后为每个要处理的字符调用例程。
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
当然,您可以先将所有字符收集到一个数组或集合中,然后对其进行循环,但我认为这样做没有任何意义。