Positioning a replacement Table of Contents?

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)?
 
S

Stefan Blom

You need to specify the start of the document in the Range parameter
of the Add method. For example, you can use

Range:=ActiveDocument.Range(0,0)

(instead of Range:=Selection.Range, which references the Range object
of the current selection).
 
S

Stefan Blom

Or to make sure that you put the new TOC exactly where the old one
was, the following code should work:

Dim tocstart As Long
With ActiveDocument
tocstart = .TablesOfContents(1).Range.Start
.TablesOfContents(1).Delete
.TablesOfContents.Add Range:= _
ActiveDocument.Range(tocstart, tocstart), _
RightAlignPageNumbers:= _
True, UseHeadingStyles:=True, UpperHeadingLevel:=1, _
LowerHeadingLevel:=2, IncludePageNumbers:=True, _
AddedStyles:=""
.TablesOfContents(1).TabLeader = wdTabLeaderDots
End With
 
J

Jonathan West

Hi Ian

Ian said:
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)?

Add the following line of code immediately below "With ActiveDocument"

.TablesOfContents(1).Range.Select
 
I

Ian

Jonathan West said:
Add the following line of code immediately below "With ActiveDocument"

.TablesOfContents(1).Range.Select
Once again, you've saved my day! (and proved yet again that I *really*
must become a hermit and *really* find out how to use Ranges :))

Thanks Jonathan.
 
I

Ian

Stefan Blom said:
Or to make sure that you put the new TOC exactly where the old one
was, the following code should work:
[Snip]

Many thanks Stefan. Food for thought.
 
M

macropod

Hi Ian,

Just out of curiosity - why do you need to replace the TOC instead of simply
updating it? This would be as simple as:
ActiveDocument.TablesOfContents(1).Update
and could be done from anywhere in the document.

Cheers
 
I

Ian

macropod said:
Hi Ian,

Just out of curiosity - why do you need to replace the TOC instead of simply
updating it? This would be as simple as:
ActiveDocument.TablesOfContents(1).Update
and could be done from anywhere in the document.

A very good question. I should have deleted the Delete method, and then
let VBA prompt me with a replacement. I would then have discovered
Update (as I have just done!)

Thanks Macropod.
 

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

double spacing content of TOC 2
Table of Contents Styles font issue 5
Table of Contents Nightmare 1
Table of Content 1
TOC problem 0
TOC Help 0
TOC Problem 0
TOC Question 3

Top