Reply
 
Thread Tools Display Modes
  #1   Report Post  
Posted to microsoft.public.word.tables
Tapio Marjomaki Tapio Marjomaki is offline
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   Report Post  
Posted to microsoft.public.word.tables
macropod macropod is offline
external usenet poster
 
Posts: 1,002
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   Report Post  
Posted to microsoft.public.word.tables
Stefan Blom Stefan Blom is offline
external usenet poster
 
Posts: 8,428
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   Report Post  
Posted to microsoft.public.word.tables
Tapio Marjomaki[_2_] Tapio Marjomaki[_2_] is offline
external usenet poster
 
Posts: 7
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   Report Post  
Posted to microsoft.public.word.tables
Tapio Marjomaki[_2_] Tapio Marjomaki[_2_] is offline
external usenet poster
 
Posts: 7
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   Report Post  
Posted to microsoft.public.word.tables
macropod macropod is offline
external usenet poster
 
Posts: 1,002
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?





Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
how can i find all words of a specific character length? upisdn Microsoft Word Help 2 August 27th 07 04:15 PM
macro asking for word to find/replace [email protected] Microsoft Word Help 3 October 28th 05 03:39 PM
Ms Word Find All option in vba Macro Karunakar New Users 5 March 24th 05 03:13 PM
How do I find my macro files for word so I can import them to my . hikingbarbiedoll Microsoft Word Help 1 March 2nd 05 10:50 PM
Where can I find Word Macro Examples? Tieu New Users 2 January 28th 05 06:54 PM


All times are GMT +1. The time now is 04:02 PM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"