|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
比正则效率要低,但是不受文档内容的限制,同时修复了5楼代码的BUG
Sub 四种标题层次设置_不受内容的限制()
Dim a, b, c, doc As Document, j&, ksr$, x&
Set doc = ActiveDocument
doc.Content.Find.Execute "^11", , , 1, , , , , , "^p", 2
doc.Content.Find.Execute "^p^w", , , 0, , , , , , "^p", 2
sr$ = "〇一二三四五六七八九十百千万亿"
r1$ = "[" & sr & "]@、": r2$ = "[((][" & sr & "]@[))]"
r3$ = "[0-9]@[、..]": r4$ = "[((][0-9]@[))]"
a = Array(r1, r2, r3, r4): b = Array(6, 5, 2, 11)
c = Array("标题 2", "标题 3", "标题 4", "标题 5")
For j = 0 To UBound(a)
With doc.Content.Find
Do While .Execute(a(j), , , 1)
With .Parent
If Not .Information(wdWithInTable) Then
x = Len(.Text): ksr = .Text
.Expand 4: .Collapse
If .MoveWhile(ksr, x) = x Then
.Expand 4
.Style = doc.Styles("" & c(j) & "")
.Font.ColorIndex = b(j)
.Collapse 0
Else
.Move 4, 1
End If
End If
End With
Loop
End With
Next
End Sub |
|