四时宝库

程序员的知识宝库

word VBA|批量加粗多个指定词语(word批量加粗段落)

任务描述:对一个word文档,指定多个需要加粗的词语,并加粗。

Sub 批量加粗多个指定词语()

Dim txt As String, reg As Object, match As Object

txt = ActiveDocument.Range.Text

Set reg = CreateObject("vbscript.regexp")

With reg

.Global = True

.IgnoreCase = False

.Pattern = "嗣同|复生"

For Each match In .Execute(txt) 'Execute(txt)代表所有匹配项的集合

With match

ActiveDocument.Range(.firstindex, .firstindex + .Length).Font.Bold = True

End With

Next

End With

End Sub

代码分析:

1 创建正则表达式对象:reg = CreateObject("vbscript.regexp")

2 得到匹配对象集合:reg.Execute(txt)

3 用一个for each循环对每一个匹配的文本执行文本加粗

程序运行效果如下:

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言
    友情链接