Word中批量刷新目录样式

提前定义好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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值