|
楼主 |
发表于 2020-2-6 14:21
|
显示全部楼层
本帖最后由 weiyingde 于 2020-2-7 18:54 编辑
小结一下
查找替换文本和格式,分两种情况:
(一)、不改变目标区域的范围,这样可以用一次性替换法。代码如下:
Sub 一次替换更改注意条款格式()
Dim rg As Range
With ActiveDocument.Content.Find
.ClearFormatting
With .Replacement
.ClearFormatting
With .Font
.ColorIndex = wdRed
.Name = "楷体"
.Size = 9.5
.Bold = False
End With
End With
.Execute "注意事项:*^13[!0-9]", , , 1, , , 1, , , "^&", 2
End With
End Sub
(二)、如果要改变目标区域的范围,有两种方法。
1、如果被查找的只有一处,可用下面的代码。
Sub 更改注意条款格式()
Dim rg As Range
With ActiveDocument.Content.Find
.ClearFormatting
.Execute "注意事项:*^13[!0-9]", , , 1
Set rg = .Parent
With rg
'.SetRange rg.Start + 5, rg.End - 1
With Range(.Start, .Start + 5).Font
.Name = "方正苏新诗柳楷简体"
.ColorIndex = wdDarkRed
.Size = 14
End With
.MoveStart 1, 5: .MoveEnd 1, -1
With .Font
.ColorIndex = wdRed
.Name = "楷体"
.Size = 9.5
.Bold = False
End With
End With
End With
End Sub
2、若被查找的内容有多处,必须用循环。Do……Loop。
Sub 更改多处注意条款格式()
Dim rg As Range
With ActiveDocument.Content.Find
.ClearFormatting
Do While .Execute("注意事项:*^13[!0-9]", , , 1)
Set rg = .Parent
With rg
'.SetRange rg.Start + 5, rg.End - 1
.MoveStart 1, 5: .MoveEnd 1, -1
With .Font
.ColorIndex = wdRed
.Name = "楷体"
.Size = 9.5
.Bold = False
End With
.Collapse 0
End With
Loop
End With
End Sub
注意:根据测试结果,第一种方法,.Execcute在后面;而第二种,.Execcute在前面。
|
|