提前定义好Office内置的’标题1‘~’标题9‘的样式作为目录刷新的基准样式,设置好后,执行以下脚本验证是否设置成功,基准样式呈现的宏脚本如下:
Sub 展示基准标题样式()
'展示设置后的标题样式
Selection.Style = ActiveDocument.Styles("标题 1")
Selection.TypeText Text:="一级标题"
Selection.TypeParagraph
Selection.Style = ActiveDocument.Styles("标题 2")
Selection.TypeText Text:="二级标题"
Selection.TypeParagraph
Selection.TypeText Text:="三级标题"
Selection.Style = ActiveDocument.Styles("标题 3")
Selection.TypeParagraph
Selection.TypeText Text:="四级标题"
Selection.Style = ActiveDocument.Styles("标题 4")
Selection.TypeParagraph
Selection.TypeText Text:="五级标题"
Selection.Style = ActiveDocument.Styles("标题 5")
Selection.TypeParagraph
Selection.TypeText Text:="六级标题"
Selection.Style = ActiveDocument.Styles("标题 6")
Selection.TypeParagraph
Selection.Style = ActiveDocument.Styles("标题 7")
Selection.TypeText Text:="七级标题"
Selection.TypeParagraph
Selection.Style = ActiveDocument.Styles("标题 8")
Selection.TypeText Text:="八级标题"
Selection.TypeParagraph
Selection.Style = ActiveDocument.Styles("标题 9")
Selection.TypeText Text:="九级标题"
End Sub
若基准样式不满足要求,可以直接在展示的结果中进行修改,完成修改后,执行以下脚本,即可自动按照已设置好的基准样式,批量刷新整个文档的目录样式。
Sub 刷新标题样式()
'
' 刷新标题样式 宏
'
'
On Error Resume Next
Application.ScreenUpdating = False
t = Timer
Dim s As Paragraph
For Each s In ActiveDocument.Paragraphs
If s.OutlineLevel = 10 Then
ElseIf s.OutlineLevel = wdOutlineLevel1 Then
s.Range.ParagraphFormat.Style = ActiveDocument.Styles("标题 1")
ElseIf s.OutlineLevel = wdOutlineLevel2 Then
s.Range.ParagraphFormat.Style = ActiveDocument.Styles("标题 2")
ElseIf s.OutlineLevel = wdOutlineLevel3 Then
s.Range.ParagraphFormat.Style = ActiveDocument.Styles("标题 3")
ElseIf s.OutlineLevel = wdOutlineLevel4 Then
s.Range.ParagraphFormat.Style = ActiveDocument.Styles("标题 4")
ElseIf s.OutlineLevel = wdOutlineLevel5 Then
s.Range.ParagraphFormat.Style = ActiveDocument.Styles("标题 5")
ElseIf s.OutlineLevel = wdOutlineLevel6 Then
s.Range.ParagraphFormat.Style = ActiveDocument.Styles("标题 6")
ElseIf s.OutlineLevel = wdOutlineLevel7 Then
s.Range.ParagraphFormat.Style = ActiveDocument.Styles("标题 7")
ElseIf s.OutlineLevel = wdOutlineLevel8 Then
s.Range.ParagraphFormat.Style = ActiveDocument.Styles("标题 8")
ElseIf s.OutlineLevel = wdOutlineLevel9 Then
s.Range.ParagraphFormat.Style = ActiveDocument.Styles("标题 9")
End If
Next
Application.ScreenUpdating = True
End Sub