Table of Contents Creation

F

Fred

Hello,
I'm trying to make a macro create a TOC, but I do not want
that TOC to use the normal Headings(Heading 1, through
Heading 9). Instead, I wish the TOC to use a particular
heading of my creation(It's called TOC). Creating the TOC
by hand is quite simple in word, and I can deselct the
Heading 1 through Heading 9, and have word only use my
created "TOC" heading, however, getting the macro to do it
isn't working. Thus far my code is this:
With ActiveDocument
.TablesOfContents.Add Range:=Selection.Range,
RightAlignPageNumbers:= _
True, AddedStyles _
:="TOC,1", LowerHeadingLevel:=1,
UpperHeadingLevel:=1, UseHeadingStyles:= _
False, IncludePageNumbers:=True,
UseHyperlinks:=True, HidePageNumbersInWeb:=True
.TablesOfContents(1).TabLeader = wdTabLeaderDots
.TablesOfContents.Format = wdIndexIndent
End With

What this does for me is create a TOC, but it ends up
listing out both my created heading "TOC", and Heading 1.
All I want to do is to get word to stop including the
Heading 1.

Any suggestions would be great.

Fred
 
M

Margaret Aldis

Hi Fred

I believe your problem is that you are creating a TOC based on 'levels' not
on styles. Heading 1s are always at level 1, so doing this will
automatically pick up the built-in headings as well as anything else at that
level.

Unless you have some special reason to do this in a macro, I would have
thought a better solution was to create the TOC field using the dialog
and/or hand editing the flags, store the field as AutoText, and then insert
the AutoText? At the least, have a look at the field code you can create by
hand, so you can check your macro result against it.
 
M

Margaret Aldis

Hi Fred

I'm afraid I still don't follow the logic of building the TOC field via the
macro and not using the macro to insert an AutoText saved in the
company-wide template. This is the code I use for a similar job:

Sub InsertTableOfContents(AutoTextName As String)
' inserts the TOC AutoText at the bookmark or current cursor position if
none
On Error Resume Next ' Ignore error if bookmark doesn't exist
Selection.GoTo What:=wdGoToBookmark, Name:="Contents"
ActiveDocument.AttachedTemplate.AutoTextEntries( _
AutoTextName).insert Where:=Selection.Range, RichText:= _
True
ActiveDocument.TablesOfContents(1).Update
ActiveDocument.Fields.Update ' to recalculate blank backs
End Sub

If this is no good then perhaps a workaround for you would be to add the TOC
as a field, so you can specify the flags (as they appear when you create
what you want manually) explicitly?

I've never used this one but from the Help:

Adds a Field object to the Fields collection. Returns the Field object at
the specified range.
expression.Add(Range, Type, Text, PreserveFormatting)

expression Required. An expression that returns a Fields object.

Range Required Range object. The range where you want to add the field. If
the range isn't collapsed, the field replaces the range.

Type Optional Variant. Can be any WdFieldType constant. For a list of valid
constants, consult the Object Browser. The default value is wdFieldEmpty.

(Field type for TOC appears to be wdFieldTOC)

Text Optional Variant. Additional text needed for the field. For example,
if you want to specify a switch for the field, you would add it here.

PreserveFormatting Optional Variant. True to have the formatting that's
applied to the field preserved during updates.
 

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

Similar Threads

Table of Content 1
Table of Contents Styles font issue 5
Table of Contents Nightmare 1
TOC problem 0
TOC Help 0
TOC Problem 0
TOC Question 3
Table of Contents, adjustment to the left, word 2010 7

Top