我的 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)
但是,当它运行时,我收到此错误。
该问题是由命名 sub 引起的UpdateFields。由于未知原因,该名称在 Word VBA 中被保留。将子文件重命名为允许的名称(例如FieldUpdates),代码将正常工作。