title: Word & Python - Create Table of Contents
body: <p>I'm using the pywin32.client extension for python and building a Word document. I have tried a pretty good host of methods to generate a ToC but all have failed. </p>
  
  <p>I think what I want to do is call the ActiveDocument object and create one with something like this example from the MSDN page:</p>
  
  <pre><code>Set myRange = ActiveDocument.Range(Start:=0, End:=0) 
  ActiveDocument.TablesOfContents.Add Range:=myRange, _ 
   UseFields:=False, UseHeadingStyles:=True, _ 
   LowerHeadingLevel:=3, _ 
   UpperHeadingLevel:=1
  </code></pre>
  
  <p>Except in Python it would be something like:</p>
  
  <pre><code>wordObject.ActiveDocument.TableOfContents.Add(Range=???,UseFiles=False, UseHeadingStyles=True, LowerHeadingLevel=3, UpperHeadingLevel=1)
  </code></pre>
  
  <p>I've built everything so far using the 'Selection' object (example below) and wish to add this ToC after the first page break. </p>
  
  <p>Here's a sample of what the document looks like:</p>
  
  <pre><code>objWord = win32com.client.Dispatch("Word.Application")
  objDoc = objWord.Documents.Open('pathtotemplate.docx') #
  objSel = objWord.Selection
  #These seem to work but I don't know why...
  objWord.ActiveDocument.Sections(1).Footers(1).PageNumbers.Add(1,True)
  objWord.ActiveDocument.Sections(1).Footers(1).PageNumbers.NumberStyle = 57
  objSel.Style = objWord.ActiveDocument.Styles("Heading 1")
  objSel.TypeText("TITLE PAGE AND STUFF")
  objSel.InsertParagraph()
  objSel.TypeText("Some data or another"
  objSel.TypeParagraph()
  objWord.Selection.InsertBreak()
  ####INSERT TOC HERE####
  </code></pre>
  
  <p>Any help would be greatly appreciated! In a perfect world I'd use the default first option which is available from the Word GUI but that seems to point to a file and be harder to access (something about templates).</p>
  
  <p>Thanks</p>