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

Re: Table of Contents Creation by Margaret

Margaret
Thu Jul 03 17:00:37 CDT 2003

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.

--
Margaret Aldis - Microsoft Word MVP
Syntagma partnership site: http://www.syntagma.co.uk
Word MVP FAQ site: http://www.mvps.org/word



"Fred" <Fred.Herr@lmco.com> wrote in message
news:546201c341a9$5fc54c70$a401280a@phx.gbl...
> 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



Re: Table of Contents Creation by Margaret

Margaret
Fri Jul 04 04:06:46 CDT 2003

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.


"Fred" <Fred.Herr@lmco.com> wrote in message
news:559001c341bf$bed2f080$a401280a@phx.gbl...
> I do want to base the TOC on my style rather just on
> levels, but I cannot seem to do that in the macro. I do
> need to have the macro create this TOC for me.(It's for a
> company wide template) So, if you have any suggestions,
> or code to show me for a macro of this sort I would
> greatly appreciate it.
>
> thanks,
>
> Fred
>
> >-----Original Message-----
> >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.
> >
> >--
> >Margaret Aldis - Microsoft Word MVP
> >Syntagma partnership site: http://www.syntagma.co.uk
> >Word MVP FAQ site: http://www.mvps.org/word
> >
> >
> >
> >"Fred" <Fred.Herr@lmco.com> wrote in message
> >news:546201c341a9$5fc54c70$a401280a@phx.gbl...
> >> 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
> >
> >
> >.
> >