使用 VBA 在 Word 中更新/创建目录
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题可能是使用
Document_Open
- 您必须在模板中设置事件,而不是在您要打开的文档中设置事件。您还可以使用Auto_Open
宏。请注意,ActiveDocument
也可能是罪魁祸首的一部分 - 当您启动Document_Open
事件时,您打开的文档可能还不是实际的活动文档 - 您可能需要设置一个引用您正在打开的文档,例如:最后,您的宏安全设置可能禁止执行任何内容。
在所有情况下,请仔细阅读控制 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 anAuto_Open
macro. Note thatActiveDocument
may also be part of the culprit - the document you open may not yet be the actual active document when you kick theDocument_Open
event - you may need to set a reference to the document you're opening like: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.