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
Jonathan West - 01 Aug 2006 15:00 GMT
> Hi all,
>
[quoted text clipped - 4 lines]
> 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.

Signature
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
Jean-Guy Marcil - 01 Aug 2006 15:06 GMT
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

Signature
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org