locked
Generating Chapter Names in Table of contents using Macro? RRS feed

  • Question

  • Hi All,

    I am creating a template in which I have a lot of chapters. The chapter names and sections are predefined. Since it is created in a predefined format I am not allowed to give chapter numbers. It should create the table of contents when I run that template(*.dotm file). So when I create the table of contents what happens is the chapter names will come without chapter numbers. But in the case of subsections I am allowed to give numbers and it is coming properly in the TOC. IS it possible to add some code inside the module for creating table of contents so that  when creating the TOC the chapter names will have its numbers.

    I am using the following code to create the table of contents. Also I want this table of contents in the third page of the document.

    Sub CreateTableOfContents()
    
     
    
     Set rangeWord = ActiveDocument.Range(Start:=0, End:=0)
    
      ActiveDocument.TablesOfContents.Add rangeWord, UseHeadingStyles:=True, UpperHeadingLevel:=1, LowerHeadingLevel:=3, UseFields:=True, TableID:=1, RightAlignPageNumbers:=True, IncludePageNumbers:=True, AddedStyles:=False, UseHyperlinks:=True
    
     
    
    End Sub
    Please advise.
    Best Regards Hari Sukesh
    Wednesday, December 8, 2010 2:09 PM

Answers

  • You would have to use { TC } fields that contain the chapter number and its title and build your table of contents from such fields if you want the chapter number to appear in the Table of Contents, but not in the document

    -- Hope this helps.

    Doug Robbins - Word MVP,
    dkr[atsymbol]mvps[dot]org
    Posted via the Community Bridge

    "skh.hari" wrote in message news:160baf20-715a-4b51-9563-b1f8e2c90411@communitybridge.codeplex.com...

    Hi All,

    I am creating a template in which I have a lot of chapters. The chapter names and sections are predefined. Since it is created in a predefined format I am not allowed to give chapter numbers. It should create the table of contents when I run that template(*.dotm file). So when I create the table of contents what happens is the chapter names will come without chapter numbers. But in the case of subsections I am allowed to give numbers and it is coming properly in the TOC. IS it possible to add some code inside the module for creating table of contents so that  when creating the TOC the chapter names will have its numbers.

    I am using the following code to create the table of contents. Also I want this table of contents in the third page of the document.

    Sub CreateTableOfContents()
    
    
    
    Set rangeWord = ActiveDocument.Range(Start:=0, End:=0)
    
     ActiveDocument.TablesOfContents.Add rangeWord, UseHeadingStyles:=True, UpperHeadingLevel:=1, LowerHeadingLevel:=3, UseFields:=True, TableID:=1, RightAlignPageNumbers:=True, IncludePageNumbers:=True, AddedStyles:=False, UseHyperlinks:=True
    
    
    
    End Sub

    Please advise. -- Best Regards Hari Sukesh


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    • Marked as answer by Bessie Zhao Wednesday, December 15, 2010 7:44 AM
    Thursday, December 9, 2010 1:49 AM