如何更新 Word 文档中的所有字段?

Gil*_*il' 102 field-codes microsoft-word

我想要一种更新Word 2013 文档中所有字段的方法。(如果它适用于其他版本,那就更好了;我最初在 Word 2007 中遇到了这个问题,从那时起似乎没有任何变化。)这包括交叉引用、页码、目录、索引、标题等。如果它可以通过按 更新F9,我希望它更新。

(理论上更新字段会导致其他字段需要更新,例如较长的目录会更改正文中的某些页码。处理常见情况对我来说已经足够了。实际上,如果我必须运行也可以宏在稳定之前两次或三次。我只想有一个可以找到所有内容的宏。)

到目前为止,我的尝试没有更新图形内文本框中的字段。我如何更新它们,我还错过了什么?


编辑:将给出的答案与我已经拥有的答案相结合,给出了一个似乎可以更新所有内容(具有已知缺陷)的宏。

'' Update all the fields, indexes, etc. in the specified document.
Sub UpdateAllFieldsIn(doc As Document)
    '' Update tables. We do this first so that they contain all necessary
    '' entries and so extend to their final number of pages.
    Dim toc As TableOfContents
    For Each toc In doc.TablesOfContents
        toc.Update
    Next toc
    Dim tof As TableOfFigures
    For Each tof In doc.TablesOfFigures
        tof.Update
    Next tof
    '' Update fields everywhere. This includes updates of page numbers in
    '' tables (but would not add or remove entries). This also takes care of
    '' all index updates.
    Dim sr As range
    For Each sr In doc.StoryRanges
        sr.Fields.Update
        While Not (sr.NextStoryRange Is Nothing)
            Set sr = sr.NextStoryRange
            '' FIXME: for footnotes, endnotes and comments, I get a pop-up
            '' "Word cannot undo this action. Do you want to continue?"
            sr.Fields.Update
        Wend
    Next sr
End Sub
'' Update all the fields, indexes, etc. in the active document.
'' This is a parameterless subroutine so that it can be used interactively.
Sub UpdateAllFields()
    UpdateAllFieldsIn ActiveDocument
End Sub
Run Code Online (Sandbox Code Playgroud)

DMA*_*361 85

我只是做Ctrl+ A- 选择一切 -然后 F9更新很多。

虽然,这会遗漏页眉和页脚,但它们会在您打印/打印预览 IIRC 时更新。


###更新

我找到了以下宏。在快速测试中,它更新了目录、段落中的字段、页眉和页脚中的字段以及浮动文本框图形中的字段。

希望这涵盖了您需要的所有内容,如果没有,请指出仍然无法更新的内容。

来源:http : //www.gmayor.com/installing_macro.htm

Sub UpdateAll()
    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
End Sub
Run Code Online (Sandbox Code Playgroud)


Dav*_*sel 37

进入打印设置,选择更新字段。然后去打印,或打印预览您的文档。

等等,所有字段都更新了!

Word of Mac 2016 中的 MS Word 打印选项

  • 现在在 Word 2010 中为我工作(设置在“文件→选项→显示”中)。事实上,如果没有选项 *某些* 字段会更新但不是全部。我很确定它在 Word 2007 中没有,但我不再需要测试它。 (2认同)
  • 我使用的是 Word 2016 for Mac。设置在 Word -&gt; 首选项 -&gt; 打印中。但在寡妇上,它将在全局设置的打印部分。我确定我过去去过那里,但我现在没有要测试的安装。 (2认同)

小智 5

这个页面看起来很有趣:

如果您使用的是 Word 2007,则过程略有不同:单击 Office 按钮,然后单击 Word 选项。Word 显示 Word 选项对话框。单击对话框左侧的高级。(单击此处查看相关图。)在“常规”区域(向下滚动一点以查看)中,确保选中“打开时自动更新链接”复选框。单击确定。该设置应确保您的所有链接始终是最新的。如果要在打开文档时更新字段,则需要使用宏来完成该任务。具体来说,您需要使用 AutoOpen 或 AutoClose 宏,具体取决于您是要在文档打开还是关闭时更新字段。以下是您可以使用的 AutoOpen 宏的示例。

Sub AutoOpen()
    With Options
        .UpdateFieldsAtPrint = True
        .UpdateLinksAtPrint = True
    End With
    ActiveDocument.Fields.Update
End Sub
Run Code Online (Sandbox Code Playgroud)

请注意,宏确保将选项设置为在打印时强制更新字段和链接,然后它会更新文档中 Fields 集合的所有成员。相反,如果您想在关闭时更新字段,则可以使用此宏:

Sub AutoClose()
    ActiveDocument.Fields.Update
End Sub
Run Code Online (Sandbox Code Playgroud)

此宏要短得多,因为在退出文档时无需设置打印时更新选项。请退出文档。