I'm using Word 2010 (32-bit) with Win7 Home Premium (64-bit).
Just recently, a couple macros I've been using for years to update TOCs have stopped working, and the failure isn't limited to any particular document.
If I click "Update Table" from the ribbon, the TOCs update fine, but if I run a macro with "ActiveDocument.TablesOfContents(1).Update", the TOC doesn't update.
Anybody else having this problem, or know of a possible solution?
Updating TOC with VBA has stopped working
-
- NewLounger
- Posts: 13
- Joined: 10 Jan 2011, 03:12
-
- Administrator
- Posts: 78478
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Updating TOC with VBA has stopped working
That's strange. Just to make sure, I assume that the following doesn't work either?
Code: Select all
Sub UpdateAllTOCs()
Dim toc as TableOfContents
For each toc in ActiveDocument.TablesOfContents
toc.Update
Next toc
End Sub
Best wishes,
Hans
Hans
-
- NewLounger
- Posts: 13
- Joined: 10 Jan 2011, 03:12
Re: Updating TOC with VBA has stopped working
I hadn't tried that, but your assumption was correct: it also fails.HansV wrote:That's strange. Just to make sure, I assume that the following doesn't work either?
Code: Select all
Sub UpdateAllTOCs() Dim toc as TableOfContents For each toc in ActiveDocument.TablesOfContents toc.Update Next toc End Sub
-
- NewLounger
- Posts: 13
- Joined: 10 Jan 2011, 03:12
Re: Updating TOC with VBA has stopped working
More info, in case it's useful.
I created a new doc with a TOC and a few headings, and then tried to record a new macro (saved in the new doc) that updated the TOC (by using "Update Table" on the ribbon as I recorded).
I also typed some text as part of the recording, and when I looked at the new macro in the IDE, the text-typing is there, but there's nothing that reflects the updating of the TOC.
Then I tried recording a new macro where I added a new TOC (replacing the old) using the ribbon, and that macro recorded perfectly:
I created a new doc with a TOC and a few headings, and then tried to record a new macro (saved in the new doc) that updated the TOC (by using "Update Table" on the ribbon as I recorded).
I also typed some text as part of the recording, and when I looked at the new macro in the IDE, the text-typing is there, but there's nothing that reflects the updating of the TOC.
Then I tried recording a new macro where I added a new TOC (replacing the old) using the ribbon, and that macro recorded perfectly:
Code: Select all
Selection.TypeText Text:="Test type"
Selection.TypeParagraph
With ActiveDocument
.TablesOfContents(1).Delete
.TablesOfContents.Add Range:=Selection.Range, RightAlignPageNumbers:= _
True, UseHeadingStyles:=True, UpperHeadingLevel:=1, _
LowerHeadingLevel:=3, IncludePageNumbers:=True, AddedStyles:="", _
UseHyperlinks:=True, HidePageNumbersInWeb:=True, UseOutlineLevels:= _
True
.TablesOfContents(1).TabLeader = wdTabLeaderDots
.TablesOfContents.Format = wdIndexIndent
End With
-
- Administrator
- Posts: 78478
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Updating TOC with VBA has stopped working
I'm using Word 2019 (on Windows 10). I can confirm that updating the TOC isn't being recorded by the macro recorder.
ActiveDocument.TablesOfContents(1).Update
does work as intended, though: after editing some of the headings and their location, the above line updates both the text and the page numbers in the TOC.
You might try repairing your Office installation from the Programs and Features control panel.
ActiveDocument.TablesOfContents(1).Update
does work as intended, though: after editing some of the headings and their location, the above line updates both the text and the page numbers in the TOC.
You might try repairing your Office installation from the Programs and Features control panel.
Best wishes,
Hans
Hans
-
- NewLounger
- Posts: 13
- Joined: 10 Jan 2011, 03:12
Re: Updating TOC with VBA has stopped working
I just tried that, but it didn't fix the issue. Thanks for your prompt responses, tho, as always.HansV wrote:You might try repairing your Office installation from the Programs and Features control panel.
I installed the May Office 2010 Security Update on May 16, and I'm pretty sure this problem doesn't go that far back, but I'm wondering if I should try uninstalling that update.
What I should probably do is just sit tight at this point and wait a week or two or three, to see if it's a problem being experienced by others and if some kind of fix is in the works.
-
- Administrator
- Posts: 78478
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Updating TOC with VBA has stopped working
It won't hurt to uninstall that update - if it doesn't solve the problem, you can always reinstall it...
Best wishes,
Hans
Hans