Style of para after TOC

K

Ken Endacott

How do I find the style of the paragraph immediately after a Table of
Contents?
 
J

Jay Freedman

Sub x()
Dim myRg As Range
If ActiveDocument.TablesOfContents.Count > 0 Then
Set myRg = ActiveDocument.TablesOfContents(1).Range
myRg.Collapse wdCollapseEnd
myRg.Move wdParagraph, 1 ' goes to next paragraph
MsgBox myRg.Style
Else
MsgBox "No TOC found"
End If
End Sub

If there may be more than one TOC in the document, modify the setting
of myRg to use the correct one (how you do that depends on how they're
identified).

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
K

Ken Endacott

Jay

Your suggested code does not give the style of the paragraph
immediately after a TOC.

I am trying to determine the style of each paragraphs in the document
with the following code. It raises an error message on the paragraph
immediately after a TOC. I can use On Error to trap the error and
advance to the next paragraph but I cannot get the style of the
offending paragraph.

VBA thinks that the paragraph has no style even though Normal view
shows a style.

Sub x()
Dim aPara As Paragraph
Dim sName As String
For Each aPara In ActiveDocument.Range.Paragraphs
sName = aPara.Range.Style
Next aPara
End Sub
 
S

Stefan Blom

I think the problem is the paragraph mark following the TOC field. Most likely,
the code cannot determine if the mark belongs to the TOC (field) or not... If
you show/hide nonprinting marks (Ctrl+Shift+8) as well as field codes (Alt+F9),
that may shed some light on what is happening.

I don't have a solution, though. :-(
 
J

Jay Freedman

Hmm, interesting. Word 2010 is different from Word 2007 in this area.
When you use the Table of Contents gallery in 2007, it just inserts a
TOC heading, the TOC field, and one empty Normal-style paragraph into
the document. When you do the same thing in 2010, it inserts those
three items inside a content control.

I think (but I'm not sure) that content control is why your macro says
the paragraph has no style (technically, the expression
aPara.Range.Style returns the value Nothing and gets the error "Object
variable or With block variable not set").

However, if you replace that expression with aPara.Style, the macro
behaves perfectly.

Or, if you forget about the canned TOCs in the Building Blocks gallery
and instead just insert a TOC field through the Insert > Quick Parts >
Fields dialog, your original macro behaves correctly.

Just another "improvement". Sorry...

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
S

Stefan Blom

Hmm, in my experience, Word 2007 makes use of a content control too (well, not
if you use References tab | Table of Contents | Insert Table of Contents).

For what it's worth, I inserted a TOC manually and I got the result described by
the OP: code stops executing.
 
K

Ken Endacott

I am trying to write code that will run under 2003, 2007 and, when I
install it, 2010.

In 2007, the extra empty paragraph with Normal style inserted after
the TOC when using the References tab seems to fix the problem for
2007. This would explain why inserting a TOC manually or operating on
a document prepared in 2003, will give the error.

It looks as though I will have to use an error trap to step over the
offending paragraph which may give different results in different Word
versions.

It worries me that 2010 does things differently. I will have to wait
and see.
 

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