locked
Automating Word - Adding a Table of Contents

    Question

  • I'm using VB.Net 2010 top create a Word 2010 doc file (creating the word doc from scratch)

    I'm looking for clear and concise instructions on how to add a "table of contents"

    I have found a couple of different examples, but they were very confusing and, ultimately, didn't work.

    TIA


    Clif (Your company name here)
    Monday, June 27, 2011 5:12 PM

Answers

  • Ok, I've finally found an example that works (mostly).

        With oDoc
          .TablesOfContents.Add(Range:=oWord.Selection.Range, _
                     RightAlignPageNumbers:=True, _
                     UseHeadingStyles:=True, _
                     IncludePageNumbers:=True, _
                     AddedStyles:="styleSection", _
                     UseHyperlinks:=False, _
                     HidePageNumbersInWeb:=True, _
                     UseOutlineLevels:=True)
          .TablesOfContents(1).Range.Font.Name = "Arial Narrow"
          .TablesOfContents(1).Range.Font.Size = 11
          .TablesOfContents(1).TabLeader = Word.WdTabLeader.wdTabLeaderDots
          .TablesOfContents.Format = Word.WdTocFormat.wdTOCSimple
        End With
    

    Just a couple of issues that, hopefully, y'all can help me with...

    First, the TOC works on a simple level.  All the headings that I need on the TOC are included, all I had to do was define and add a style (styleSection) to each heading I want included.  However, I need to add an indentation level to a couple of sub-headings.  I presume I need to define another style and apply that to the sub-headings.  But how does the TOC know which what to indent?  How do I add the sub-heading style to the TOC structure above?

    Second, even though I tried I could not set the font on the TOC to what I wanted (Arial Narrow/11pt).  It seems to want to set itself to it's default (Calibri/10).  How do I set the TOC font the way I want?

    Again, TIA


    Clif (Your company name here)


    • Marked as answer by Clif001 Tuesday, June 28, 2011 5:20 PM
    Monday, June 27, 2011 8:49 PM

All replies

  • Ok, I've finally found an example that works (mostly).

        With oDoc
          .TablesOfContents.Add(Range:=oWord.Selection.Range, _
                     RightAlignPageNumbers:=True, _
                     UseHeadingStyles:=True, _
                     IncludePageNumbers:=True, _
                     AddedStyles:="styleSection", _
                     UseHyperlinks:=False, _
                     HidePageNumbersInWeb:=True, _
                     UseOutlineLevels:=True)
          .TablesOfContents(1).Range.Font.Name = "Arial Narrow"
          .TablesOfContents(1).Range.Font.Size = 11
          .TablesOfContents(1).TabLeader = Word.WdTabLeader.wdTabLeaderDots
          .TablesOfContents.Format = Word.WdTocFormat.wdTOCSimple
        End With
    

    Just a couple of issues that, hopefully, y'all can help me with...

    First, the TOC works on a simple level.  All the headings that I need on the TOC are included, all I had to do was define and add a style (styleSection) to each heading I want included.  However, I need to add an indentation level to a couple of sub-headings.  I presume I need to define another style and apply that to the sub-headings.  But how does the TOC know which what to indent?  How do I add the sub-heading style to the TOC structure above?

    Second, even though I tried I could not set the font on the TOC to what I wanted (Arial Narrow/11pt).  It seems to want to set itself to it's default (Calibri/10).  How do I set the TOC font the way I want?

    Again, TIA


    Clif (Your company name here)


    • Marked as answer by Clif001 Tuesday, June 28, 2011 5:20 PM
    Monday, June 27, 2011 8:49 PM
  • Hi Clif001,

    Below code sample implements inserting a 3 x 5 table and filling it with specific data.

    oTable.Rows.Item(1).Range.Font.Bold = CInt(True) to set each row's format

    oTable.Rows.Item(1).Cells(1).Range.Font.Underline = CType(True, WdUnderline) to set each cell's format

    Please have a try and let us know the situation on your side.

    In addition, I am not sure your table structure. How do you add the sub-headings? Could you please provide a picture for the created table?

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
         ' Create Word Application
        Dim oWord As Word.Application = CType(CreateObject("Word.Application"), Application)
        ' Create new word document
        Dim oDoc As Word.Document = oWord.Documents.Add()
        oWord.Visible = True
    
        'Insert a 3 x 5 table and fill it with specific data
        Dim r As Integer, c As Integer
        Dim oTable As Word.Table = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, 3, 5)
        oTable.Range.ParagraphFormat.SpaceAfter = 6
        For r = 1 To 3
          For c = 1 To 5
            oTable.Cell(r, c).Range.Text = "Row" & r & "Col" & c
          Next
        Next
        'make the first row bold and italic
        oTable.Rows.Item(1).Range.Font.Bold = CInt(True)
        oTable.Rows.Item(1).Range.Font.Italic = CInt(True)
    
        'set the second row's font as Arial Narrow/11pt
        oTable.Rows.Item(2).Range.Font.Name = "Arial Narrow"
        oTable.Rows.Item(2).Range.Font.Size = 11
    
        ' Save this word document
        oDoc.SaveAs("C:\myfile.doc", True)
        oDoc.Close()
        oWord.Application.Quit()
    
      End Sub
    

    If you have any concerns, please feel free to follow up.

    Have a nice day.

    Best regards


    Liliane Teng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, June 28, 2011 8:04 AM
  • Hi Liliane,

    Thanks for the reply. But I'm confused. My post was about a Table of Contents, not just a table. I'm not entirely sure how your suggestion helps.

    I finally figured out how to create the TOC, but needed help in indenting some sub sections and setting the font (read my second post).



    Clif (Your company name here)

    Tuesday, June 28, 2011 12:20 PM
  • Solution Found.

    Appearently I added the line ".TablesOfContents.Format = Word.WdTocFormat.wdTOCSimple" when I didn't need to.

    Thanks for the help.


    Clif (Your company name here)
    Tuesday, June 28, 2011 5:20 PM