leedun 你好!请试试下面代码:
- Sub test多级标题()
- '预处理
- With ActiveDocument
- '回车符/手动换行符=>段落标记
- .Content.Find.Execute "[^13^11]", , , 1, , , , , , "^p", 2
- '删除所有域
- .Fields.Unlink
- '自动编号转文本
- .ConvertNumbersToText
- '多级编号后添加半角空格
- With .Content.Find
- .Execute "(^13[0-9.]{1,})", , , 1, , , , , , "\1 ", 2
- .Execute "(^13[0-9.]{1,})([ ^s^t]{1,})", , , 1, , , , , , "\1 ", 2
- End With
- End With
- '核心代码
- Dim r As Range
- With ActiveDocument.Content.Find
- .ClearFormatting
- .Text = "^13[0-9.]{1,}"
- .Forward = True
- .MatchWildcards = True
- Do While .Execute
- With .Parent
- .MoveStart
- Set r = .Paragraphs(1).Range
- If .Text Like "*.*.*.*" Then
- r.Style = wdStyleHeading5
- r.Font.Color = wdColorOrange
- ElseIf .Text Like "*.*.*" Then
- r.Style = wdStyleHeading4
- r.Font.Color = wdColorGreen
- ElseIf .Text Like "*.?*" Then
- r.Style = wdStyleHeading3
- r.Font.Color = wdColorPink
- ElseIf .Text Like "*." Then
- r.Style = wdStyleHeading2
- r.Font.Color = wdColorRed
- End If
- .Start = .End
- End With
- Loop
- End With
- End Sub
复制代码 |