Hello,

I am using Word COM to automatically add some text to page 1 of a document,
then insert a TOC (Table of Contents) on Page 2.

I am struggling on how to tell it to insert the TOC at the END of the
document (As I am building it upwards). When I try and do an insert it
deletes the text I've placed on page 1.

set objword = CreateObject("Word.Application")
objword.visible = True
set objDoc = objword.Documents.Add()

set objSelection = objWord.Selection

objselection.TypeText("FIRST PAGE TEXT HERE")
objSelection.InsertBreak(7)

set myRange = objDoc.Range(0,0)
objdoc.TablesOfContents.Add myRange

Many Thanks in advance

Mark

RE: Inserting Table of Contents @ End of Word Document by lf

lf
Tue Oct 16 04:27:02 PDT 2007

One way to solve the problem could be to replace your code line:
set myRange = objDoc.Range(0,0)
with
Set myRange = objdoc.Range.Characters.Last

--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word


"Mark Holland" wrote:

> Hello,
>
> I am using Word COM to automatically add some text to page 1 of a document,
> then insert a TOC (Table of Contents) on Page 2.
>
> I am struggling on how to tell it to insert the TOC at the END of the
> document (As I am building it upwards). When I try and do an insert it
> deletes the text I've placed on page 1.
>
> set objword = CreateObject("Word.Application")
> objword.visible = True
> set objDoc = objword.Documents.Add()
>
> set objSelection = objWord.Selection
>
> objselection.TypeText("FIRST PAGE TEXT HERE")
> objSelection.InsertBreak(7)
>
> set myRange = objDoc.Range(0,0)
> objdoc.TablesOfContents.Add myRange
>
> Many Thanks in advance
>
> Mark
>
>

Re: Inserting Table of Contents @ End of Word Document by fumei

fumei
Tue Oct 16 05:13:14 PDT 2007

When I try and do an insert it
>deletes the text I've placed on page 1.
>
>set myRange = objDoc.Range(0,0)
This sets the range for page 1!

so THIS puts theToC on Page 1, not 2
>objdoc.TablesOfContents.Add myRange

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/200710/1