You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Word宏中更新除Table of Contents外的所有域?

Word宏:实现更新除目录外所有域的功能

直接给你补全「更新除目录外所有域」的逻辑,以下是完整宏代码:

Sub RefreshFields()
'
' RefreshFields Macro
'
    Dim ToC As TableOfContents
    Dim fld As Field

    Select Case InputBox("Update which fields?" & vbCrLf & "Type '1' for all fields" & vbCrLf & "Type '2' for Table of Contents only" & vbCrLf & "Type '3' for all fields except Table of Contents")
    
    Case 1
        Selection.WholeStory
        Selection.Range.HighlightColorIndex = wdYellow
        Selection.Fields.Update
        MsgBox ("All fields updated")
        
    Case 2
        For Each ToC In ActiveDocument.TablesOfContents
            ToC.Update
        Next
        MsgBox ("Only ToC updated")
        
    Case 3
        ' 遍历所有域,跳过目录域(wdFieldTOC)
        For Each fld In ActiveDocument.Fields
            If fld.Type <> wdFieldTOC Then
                fld.Update
            End If
        Next fld
        MsgBox ("All fields except ToC updated")

    End Select
    
End Sub

实现说明

  • 放弃「选中排除目录的范围」的思路,直接遍历文档所有域并精准跳过目录域(wdFieldTOC),这种方式比范围选择更可靠,不会因目录位置、数量变化出现遗漏或误操作。
  • 如果需要额外排除表格目录(wdFieldTOF)、图表目录(wdFieldTOA)等同类域,可修改判断条件为:
    If fld.Type <> wdFieldTOC And fld.Type <> wdFieldTOF And fld.Type <> wdFieldTOA Then
    

内容的提问来源于stack exchange,提问作者Chris Houdy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 16:02:40