How to find out the length the TOC by a Word 2007 macro?

T

Tapio Marjomaki

How to solve how many lines or items exist in the TOC of a Word (2007)
document using the Basic macro language?
 
M

macropod

Hi Tapio,

Here's one way to count all TOCs in the active document:
Sub Count_TOC_Entries()
Dim i As Integer
With ActiveDocument
If .TablesOfContents.Count > 0 Then
For i = 1 To .TablesOfContents.Count
MsgBox "TOC " & i & " in """ & .Name & """ has " & _
.TablesOfContents(i).Range.Paragraphs.Count & " Entries."
Next i
Else
MsgBox "No TOCs found in " & .Name
End If
End With
End Sub

Cheers
 
S

Stefan Blom

Maybe I'm missing something, but the macro seems to count the paragraph in
which the TOC field is located too? In other words, you would have to use
..Count - 1.

--
Stefan Blom
Microsoft Word MVP


Hi Tapio,

Here's one way to count all TOCs in the active document:
Sub Count_TOC_Entries()
Dim i As Integer
With ActiveDocument
If .TablesOfContents.Count > 0 Then
For i = 1 To .TablesOfContents.Count
MsgBox "TOC " & i & " in """ & .Name & """ has " & _
.TablesOfContents(i).Range.Paragraphs.Count & " Entries."
Next i
Else
MsgBox "No TOCs found in " & .Name
End If
End With
End Sub

Cheers
--
macropod
[MVP - Microsoft Word]
-------------------------

Tapio Marjomaki said:
How to solve how many lines or items exist in the TOC of a Word (2007)
document using the Basic macro language?
 
T

Tapio Marjomaki

..TablesOfContents.Count = 0 every time I run my macro though there's TOC ...
(of three items in my case) ? It should return 3 but returns 0.

Thanks anyway, Tapio

macropod said:
Hi Tapio,

Here's one way to count all TOCs in the active document:
Sub Count_TOC_Entries()
Dim i As Integer
With ActiveDocument
If .TablesOfContents.Count > 0 Then
For i = 1 To .TablesOfContents.Count
MsgBox "TOC " & i & " in """ & .Name & """ has " & _
.TablesOfContents(i).Range.Paragraphs.Count & " Entries."
Next i
Else
MsgBox "No TOCs found in " & .Name
End If
End With
End Sub

Cheers
--
macropod
[MVP - Microsoft Word]
-------------------------

Tapio Marjomaki said:
How to solve how many lines or items exist in the TOC of a Word (2007)
document using the Basic macro language?
 
T

Tapio Marjomaki

Sorry, it works OK! I had corrupted my TOCs in my original document.

Tapio

Tapio Marjomaki said:
.TablesOfContents.Count = 0 every time I run my macro though there's TOC ...
(of three items in my case) ? It should return 3 but returns 0.

Thanks anyway, Tapio

macropod said:
Hi Tapio,

Here's one way to count all TOCs in the active document:
Sub Count_TOC_Entries()
Dim i As Integer
With ActiveDocument
If .TablesOfContents.Count > 0 Then
For i = 1 To .TablesOfContents.Count
MsgBox "TOC " & i & " in """ & .Name & """ has " & _
.TablesOfContents(i).Range.Paragraphs.Count & " Entries."
Next i
Else
MsgBox "No TOCs found in " & .Name
End If
End With
End Sub

Cheers
--
macropod
[MVP - Microsoft Word]
-------------------------

Tapio Marjomaki said:
How to solve how many lines or items exist in the TOC of a Word (2007)
document using the Basic macro language?
 
M

macropod

Well spotted Stefan!

So the line:
..TablesOfContents(i).Range.Paragraphs.Count & " Entries."
should read:
..TablesOfContents(i).Range.Paragraphs.Count -1 & " Entries."

Cheers
--
macropod
[MVP - Microsoft Word]
-------------------------

Stefan Blom said:
Maybe I'm missing something, but the macro seems to count the paragraph in
which the TOC field is located too? In other words, you would have to use
.Count - 1.

--
Stefan Blom
Microsoft Word MVP


Hi Tapio,

Here's one way to count all TOCs in the active document:
Sub Count_TOC_Entries()
Dim i As Integer
With ActiveDocument
If .TablesOfContents.Count > 0 Then
For i = 1 To .TablesOfContents.Count
MsgBox "TOC " & i & " in """ & .Name & """ has " & _
.TablesOfContents(i).Range.Paragraphs.Count & " Entries."
Next i
Else
MsgBox "No TOCs found in " & .Name
End If
End With
End Sub

Cheers
--
macropod
[MVP - Microsoft Word]
-------------------------

Tapio Marjomaki said:
How to solve how many lines or items exist in the TOC of a Word (2007)
document using the Basic macro language?
 
Top