更新word中的目录

Dan*_*Dan 3 vba ms-word

我的 Word 文档有一个宏,旨在更新所有字段和所有目录。

Sub UpdateFields()
    Dim oStory As Range
    For Each oStory In ActiveDocument.StoryRanges
        oStory.Fields.Update
        If oStory.StoryType <> wdMainTextStory Then
            While Not (oStory.NextStoryRange Is Nothing)
                Set oStory = oStory.NextStoryRange
                oStory.Fields.Update
            Wend
        End If
    Next oStory
    Set oStory = Nothing

    Dim TOC As TableOfContents
    For Each TOC In ActiveDocument.TablesOfContents
        TOC.Update
    Next
End Sub
Run Code Online (Sandbox Code Playgroud)

但是,当它运行时,我收到此错误。

在此输入图像描述
错误然后在下面进行调试,
我将不胜感激解决问题的任何帮助。

joe*_*hwa 5

该问题是由命名 sub 引起的UpdateFields。由于未知原因,该名称在 Word VBA 中被保留。将子文件重命名为允许的名称(例如FieldUpdates),代码将正常工作。