title: Adding a table of contents to a Microsoft Word Document using vbs
body: <p>I am attempting to write a vbs script to create a word document that also contains a Table of Contents.  I am able to insert a Table of Contents but am unable to populate the table.  </p>
  
  <pre><code>Set oInfo = Dictionary.Items("Use Case")
      For Each oInfo in oInfo
  
          Set entryRange = objSelection.Range
          objDoc.TablesOfContents.MarkEntry entryRange, oInfo("Name"), oInfo("Name"), W, 1
          objSelection.TypeText "Info" + vbNewline + oInfo("Name")
          objSelection.TypeParagraph()
      Next
  objDoc.TablesOfContents.Add(tocRange)
  </code></pre>
  
  <p>This results in the Table of Contents being inserted properly but only displays "<strong>No table of contents entries found.</strong>"  Also, at the end of the document, I get weird unformatted text that looks something like this:
  <strong>{TC "Info1" \f C \| "1"}</strong>
  This gets repeated for each object that I iterate through in the For loop with the correct title for each section.  I feel like I am on the right track, but I don't know enough about the <code>TablesOfContents</code> object to do what I would like to do. I believe one of the main problems is the <code>TableId</code> field in the <code>markEntry</code> function.</p>