欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

office word利用VBA给指定的多个字符字体批量标红加粗

程序员文章站 2024-02-22 21:48:13
...

office word利用VBA给指定的多个字符字体批量标红加粗
场景说明:比如我有一个英文的word文档,无需要把不认识的单词标成红色并且加粗,如果一个一个替换太麻烦,我想到用vba来实现 ,例如我要把abandon、abbreviation、abide、abiding、able、abnormal、aboard、abroad几个在文档中出现的单词标红加粗,我先定义一个字符strarr = "abandon |abbreviation |abide |abiding |able |abnormal |aboard |abroad " 单词用|线分割。
例子如下

Sub biaohong()
Dim strarr As String

strarr = "abandon |abbreviation |abide |abiding |able |abnormal |aboard |abroad "
a = Split(strarr, " ")
b = UBound(a)

  For i = 0 To b
         
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find.Replacement.Font
        .Bold = True
        .Color = wdColorRed
    End With
    With Selection.Find
        
         
        .Text = a(i)
        .Replacement.Text = a(i)
        .Forward = True
        .Wrap = wdFindAsk
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchByte = True
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    Next i
End Sub