A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Word » Tables
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

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



 
 
Thread Tools Display Modes
  #1  
Old September 11th, 2007, 11:40 AM posted to microsoft.public.word.tables
Tapio Marjomaki
external usenet poster
 
Posts: 1
Default How to find out the length the TOC by a Word 2007 macro?

How to solve how many lines or items exist in the TOC of a Word (2007)
document using the Basic macro language?
  #2  
Old September 11th, 2007, 12:10 PM posted to microsoft.public.word.tables
macropod
external usenet poster
 
Posts: 1,231
Default How to find out the length the TOC by a Word 2007 macro?

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" Tapio wrote in message
...
How to solve how many lines or items exist in the TOC of a Word (2007)
document using the Basic macro language?


  #3  
Old September 11th, 2007, 12:56 PM posted to microsoft.public.word.tables
Stefan Blom
external usenet poster
 
Posts: 8,433
Default How to find out the length the TOC by a Word 2007 macro?

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


"macropod" wrote in message ...
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" Tapio wrote in
message ...
How to solve how many lines or items exist in the TOC of a Word (2007)
document using the Basic macro language?





  #4  
Old September 11th, 2007, 06:46 PM posted to microsoft.public.word.tables
Tapio Marjomaki[_2_]
external usenet poster
 
Posts: 8
Default How to find out the length the TOC by a Word 2007 macro?

..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" wrote:

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" Tapio wrote in message
...
How to solve how many lines or items exist in the TOC of a Word (2007)
document using the Basic macro language?



  #5  
Old September 11th, 2007, 07:20 PM posted to microsoft.public.word.tables
Tapio Marjomaki[_2_]
external usenet poster
 
Posts: 8
Default How to find out the length the TOC by a Word 2007 macro?

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

Tapio

"Tapio Marjomaki" wrote:

.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" wrote:

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" Tapio wrote in message
...
How to solve how many lines or items exist in the TOC of a Word (2007)
document using the Basic macro language?



  #6  
Old September 11th, 2007, 11:05 PM posted to microsoft.public.word.tables
macropod
external usenet poster
 
Posts: 1,231
Default How to find out the length the TOC by a Word 2007 macro?

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" wrote in message ...
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


"macropod" wrote in message ...
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" Tapio wrote in
message ...
How to solve how many lines or items exist in the TOC of a Word (2007)
document using the Basic macro language?





 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 10:03 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.