I
Ian
Using Word97.
Having programmatically reformatted a document, I want to replace the
old table of contents. Doing this by hand and recording a macro gives
the following code:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
With ActiveDocument
.TablesOfContents(1).Delete
.TablesOfContents.Add Range:=Selection.Range,
RightAlignPageNumbers:= _
True, UseHeadingStyles:=True, UpperHeadingLevel:=1, _
LowerHeadingLevel:=2, IncludePageNumbers:=True, AddedStyles:=""
.TablesOfContents(1).TabLeader = wdTabLeaderDots
End With
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When running this code, the original ToC is indeed replaced, but the
replacement starts at the *current* position in the document (which
could be anywhere, depending on what the user had been doing immediately
beforehand), instead of starting where the original ToC started.
What do I need to add to the code to ensure the new ToC overwrites the
old one in the right place (like it does when you do it manually)?
Having programmatically reformatted a document, I want to replace the
old table of contents. Doing this by hand and recording a macro gives
the following code:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
With ActiveDocument
.TablesOfContents(1).Delete
.TablesOfContents.Add Range:=Selection.Range,
RightAlignPageNumbers:= _
True, UseHeadingStyles:=True, UpperHeadingLevel:=1, _
LowerHeadingLevel:=2, IncludePageNumbers:=True, AddedStyles:=""
.TablesOfContents(1).TabLeader = wdTabLeaderDots
End With
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When running this code, the original ToC is indeed replaced, but the
replacement starts at the *current* position in the document (which
could be anywhere, depending on what the user had been doing immediately
beforehand), instead of starting where the original ToC started.
What do I need to add to the code to ensure the new ToC overwrites the
old one in the right place (like it does when you do it manually)?