PDA

View Full Version : Updating a Table of Contents (TOC)



Roderick
02-25-2018, 04:46 PM
I'm using Office 2013 in Windows 10.

I've created a TOC using custom styles: GA Numbered Heading 1 to GA Numbered Heading 6. I don't want to use the inbuilt Heading 1, etc.

This is the field code structure:

{TOC \t "GA Numbered Heading 1,1,GA Numbered Heading 2,2,GA Numbered Heading 3,3,GA Numbered Heading 4,4,
GA Numbered Heading 5,5,GA Numbered Heading 6,6"}

In the body of the document I now create a paragraph of text and then format it with the GA Heading 1 style. There is a macro attached to this process of formatting a paragraph which will update the TOC.

This is the procedure to do it (and big thanks to Graham Mayor):


Sub UpdateTheTocs()
' http://www.gmayor.com/installing_macro.htm
' update TOC fields in a document
Dim oField As Field
For Each oField In ActiveDocument.Fields
If oField.Type = wdFieldTOC Then
oField.Update
End If
Next oField
Set oField = Nothing
End Sub

This works perfectly the first time. However, I now select another paragraph of text and apply the GA Numbered Heading 2 style which then calls the above procedure and all should work OK with the TOC being updated and the new paragraph displayed.

Not so. No matter what I do, the TOC field will just not update. I even try it manually by using Alt+A followed by F9. Still nothing.

Could anyone suggest, please, what is going wrong and a way to put it right?

Thanks

Roderick

gmayor
02-25-2018, 09:41 PM
Use the following code instead


Sub UpdateTOCs()
'Graham Mayor - http://www.gmayor.com - Last updated - 26 Feb 2018
Dim oTOC As TableOfContents
Dim bFound As Boolean
For Each oTOC In ActiveDocument.TablesOfContents
bFound = True
oTOC.Update
Next oTOC
If Not bFound Then
MsgBox "No TOC Found"
End If
lbl_Exit:
Set oTOC = Nothing
Exit Sub
End Sub

Roderick
05-04-2018, 04:45 AM
In February of this year a great answer was given by Graham Mayor as seen above to update my TOCs.

Unfortunately, this problem has reared its head again in the following way:

I have a template with two TOCs and I want to update these with the following code (as above)

Sub UpdateTOCs()
'Graham Mayor - http://www.gmayor.com - Last updated - 26 Feb 2018
Dim oTOC As TableOfContents
Dim bFound As Boolean
For Each oTOC In ActiveDocument.TablesOfContents
bFound = True
oTOC.Update
Next oTOC
If Not bFound Then
MsgBox "No TOC Found"
End If
lbl_Exit:
Set oTOC = Nothing
Exit Sub
End Sub




I step through the code and when it gets to the line
oTOC.Update
it just stops completely and doesn't move on to the next line. It's as if the procedure has finished.

Can anyone shed some light on what is happening, please?