WordAutomation: TableOfContents

My app creates a new Word document(docx) with the use of a template (docm).
The template has a table of contents like this:

Table of Contents
1 Customizing Settings in IMG…7
A Other applicable documents…8
List of Abbreviations…9
References…10
Change History / Release Notes…11
Distribution List…12
Authorization…12

My app inserts into the new document some header with content. The headers looks like this

1.1 Customizing this and that
1.1.1 Customizing Activities
1.1.1.1 Usage
1.1.1.2 Prerequisites
1.1.1.3 Database Entities

And this hierarchy is repeated for many entries.

Now I want to update the table of contents.
With

oWord.ActiveDocument.TablesOfContents.add(oWord.ActiveDocument.Range(0,0),true,1,9,false,nil,true,true,nil,true,true,false)

I get the same as the template already has.

With

oWord.ActiveDocument.TablesOfContents(1).Update

I get only lower header lines like this

Table of Contents
1.1.1 Customizing Activities – due to Customizing Object [Warehouse Process Type]…7
1.2.1 Customizing Activities – due to Customizing Object [Warehouse Process Type - Description] …7
1.3.1 Customizing Activities – due to Customizing Object [Storage Process - Definition]…8
1.4.1 Customizing Activities – due to Customizing Object [Storage Process - Description]…9
1.5.1 Customizing Activities – due to Customizing Object [Determination of Warehouse Process Type]…9

Has anybody an idea how I get the complete list of headings into the table of contents?

Are they in the same styles, TOC uses styles for selecting the headings, have they been missed?

Whenever I have any question about MS Office Automation, I refer to my library of e-books from Eugene Daken. I’ve never had a case yet where that didn’t solve my issue. He has one on MS Word:

I Wish I Knew How To …
Program Word 2010-2016
with Xojo in Windows
January 2019 Edition (2.5)
By Eugene Dakin

Glancing at my copy of it, there’s a whole chapter (Chapter 6) dedicated to creating and updating a Table of Contents. Although the chapter is not terribly long (8 pages in total), he does cover the following topics with examples:

  • Basic Table of Contents
  • Advanced Table of Contents
  • Refresh

Eugene’s e-books can be found in the Xojo Store and are REALLY a proverbial “cheap date” (you would spend more at McDonalds for a family of three than you would for any of his topical e-books). Even if this didn’t result in resolving your problem directly , you’ll find it serves as an invaluable resource many times over for other MS Automation questions that arise…

I know that doesn’t answer your immediate question, but I hope it helps and leads to an answer for you.

That is how I used Word 35 years ago to achieve that goal.

and it still works today :slight_smile:

I have the e-book “Program Word with Xojo in Windows” of Eugene Dakin and it is very helpful, but I can’t find the solution in there.

The template has the styles “H1”, “H2”, “H3”, “berschrift 4”. The template is given, i can’t change it. May be I had to build an own collection of heading styles with the HeadingStyles object?

The TOC looks at the styles in the document to build, as you know, H1, H2 etc. Once added then an update of the TOC field, I right click and select update field rebuilds the TOC. There is likely some VBA that could trigger an update of the TOC field object in a similar way but I dont know what it may be off the top of my head.

Hello Michael,

This is a difficult question that I haven’t been able to figure out in Xojo-code yet. You have done well by formatting the headers correctly, and here is the code in VBA that needs to be converted to Xojo code:

Application.Templates( _ "C:\\Users\\eugen\\AppData\\Roaming\\Microsoft\\Document Building Blocks\\1033\\16\\Built-In Building Blocks.dotx" _ ).BuildingBlockEntries("Automatic Table 2").Insert Where:=Selection.Range _ , RichText:=True

This requires the use of a template that is located on your local computer called Built-In Building Blocks.dotx. I wish I had a Xojo-working answer for you.