使用 VBA 在 Word 中更新/创建目录

发布于 2024-09-06 18:01:06 字数 224 浏览 2 评论 0原文

我是 VBA 新手。每次打开Word文档时,我都尝试更新该文档中的目录,但它似乎根本没有更新。

ThisDocument 中,我完成了以下操作:

Private Sub Document_Open()
ActiveDocument.TablesOfContents(1).Update
End Sub

有人可以帮助我吗?

I'm new to VBA. I'm trying to update the Table of Contents in my Word document everytime I open the document, but it does not seem to update it at all.

In ThisDocument I've done the following:

Private Sub Document_Open()
ActiveDocument.TablesOfContents(1).Update
End Sub

Can anyone help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滥情哥ㄟ 2024-09-13 18:01:06

问题可能是使用 Document_Open - 您必须在模板中设置事件,而不是在您要打开的文档中设置事件。您还可以使用 Auto_Open 宏。请注意,ActiveDocument 也可能是罪魁祸首的一部分 - 当您启动 Document_Open 事件时,您打开的文档可能还不是实际的活动文档 - 您可能需要设置一个引用您正在打开的文档,例如:

Dim doc As Document
set doc = Documents.Open(your path here)
doc.TablesOfContents(1).Update

最后,您的宏安全设置可能禁止执行任何内容。

在所有情况下,请仔细阅读控制 Microsoft Word Through 事件在创建、打开文档或已关闭。

The issue is probably the use of Document_Open - you have to set up the event in the template, not the document you are opening. You can also use an Auto_Open macro. Note that ActiveDocument may also be part of the culprit - the document you open may not yet be the actual active document when you kick the Document_Open event - you may need to set a reference to the document you're opening like:

Dim doc As Document
set doc = Documents.Open(your path here)
doc.TablesOfContents(1).Update

Finally, your Macro Security settings could be disallowing anything from executing.

In all cases, have a good read of Take Control of Microsoft Word Through Events and Running a macro automatically when a document is created, opened or closed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文