Move cursor to end of table of contents

T

TimDouglas

Hi all,

I have a macro which adds extra text into the table of contents, but for it
to work correctly, the cursor needs to be placed at the end of the TOC. Is
there a way I can get word to place the cursor there automatically, rather
than making the user do it?

Thanks a lot,

TimDouglas
 
J

Jonathan West

TimDouglas said:
Hi all,

I have a macro which adds extra text into the table of contents, but for
it
to work correctly, the cursor needs to be placed at the end of the TOC.
Is
there a way I can get word to place the cursor there automatically, rather
than making the user do it?


Don't use the Selection object at all. Word instead with a Range object,
which is very much like the selection except that you can define as many of
them as you like, and the cursor doesn't move around when you change them.
Just about every element in a Word document has a Range property. So, for
instance, you could do this

Dim oRange as Range
Set oRange = ActiveDocument.TablesOfContents(1).Range

Now, oRange is "marking" the whole of the table of contents. You can use the
InsertAfter method to insert text after the table of contents, or
InsertBefore to insert text before it.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
J

Jean-Guy Marcil

TimDouglas was telling us:
TimDouglas nous racontait que :
Hi all,

I have a macro which adds extra text into the table of contents, but
for it to work correctly, the cursor needs to be placed at the end of
the TOC. Is there a way I can get word to place the cursor there
automatically, rather than making the user do it?

Dim rgePostTOC As Range

Set rgePostTOC = ActiveDocument.TablesOfContents(1).Range

With rgePostTOC
.Collapse wdCollapseEnd
End With

will place the range right after the TOC, then, if you are adding text, use
something like:

Dim rgePostTOC As Range

Set rgePostTOC = ActiveDocument.TablesOfContents(1).Range

With rgePostTOC
.Collapse wdCollapseEnd
.InsertAfter "New text"
End With


--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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