ToC: Testing for Table of Contents existence

P

Paul B

Hi,

I've got a macro that updates existing Word ToC's. I'd like to
enhance it by first testing for the existence of a ToC. If one
exists, it would be updated; if not, one would be created.

Is such a test possible?

Thanks,
Paul
 
J

Jean-Guy Marcil

Paul B said:
Hi,

I've got a macro that updates existing Word ToC's. I'd like to
enhance it by first testing for the existence of a ToC. If one
exists, it would be updated; if not, one would be created.

Is such a test possible?

Maybe this will be useful:


Sub ManageTOC()

Dim tocMain As TableOfContents
Dim rgeTOC As Range

If ActiveDocument.TablesOfContents.Count > 0 Then
Set tocMain = ActiveDocument.TablesOfContents(1)
Set rgeTOC = tocMain.Range
Else
'Determine where to insert TOC
Set rgeTOC = ActiveDocument.Range.Paragraphs(1).Range
With rgeTOC
.Collapse wdCollapseStart
.Fields.Add rgeTOC, wdFieldEmpty, _
"TOC \o " & Chr(34) & "1-3" & Chr(34) & "\h \z \u", False
End With
Set tocMain = ActiveDocument.TablesOfContents(1)
Set rgeTOC = tocMain.Range
End If

End Sub
 
P

Paul B

Sub ManageTOC()

Dim tocMain As TableOfContents
Dim rgeTOC As Range

If ActiveDocument.TablesOfContents.Count > 0 Then
Set tocMain = ActiveDocument.TablesOfContents(1)
Set rgeTOC = tocMain.Range
Else
'Determine where to insert TOC
Set rgeTOC = ActiveDocument.Range.Paragraphs(1).Range
With rgeTOC
.Collapse wdCollapseStart
.Fields.Add rgeTOC, wdFieldEmpty, _
"TOC \o " & Chr(34) & "1-3" & Chr(34) & "\h \z \u", False
End With
Set tocMain = ActiveDocument.TablesOfContents(1)
Set rgeTOC = tocMain.Range
End If

End Sub


Fantastic! Thanks so much.

Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top