Updating/creating Table of Contents in Word using VBA
I'm new to VBA. I开发者_如何学C'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?
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.
精彩评论