Christian
Christian

Reputation: 1735

What VBA code should be used in Microsoft Word 2007 Macro to create Table of Contents

I would like to define a macro in Microsoft Word 2007 that inserts a table of contents with the provided automatic styles when a hotkey is pressed. I successfully defined a macro to insert a non-styled (e.g. basic) table of contents as follows:

Sub InsertTableOfContents()
'
' InsertTableOfContents Macro
'
'
    With ActiveDocument
        .TablesOfContents.Add Range:=Selection.Range, RightAlignPageNumbers:= _
            True, UseHeadingStyles:=True, UpperHeadingLevel:=1, _
            LowerHeadingLevel:=3, IncludePageNumbers:=True, AddedStyles:="", _
            UseHyperlinks:=True, HidePageNumbersInWeb:=True, UseOutlineLevels:= _
            True
        .TablesOfContents(1).TabLeader = wdTabLeaderDots
        .TablesOfContents.Format = wdIndexIndent
    End With
End Sub

However, when I attempt to insert a styled table of contents as follows:

Sub InsertStyledTOC()
'
' Macro to insert a table of contents styled like Automatic Table 2
'
ActiveDocument.AttachedTemplate.BuildingBlockEntries("Automatic Table 2"). _
    Insert Where:=Selection.Range, RichText:=True
End Sub

I get the following error:

Run-time error 5941 The requested member of the collection does not exist

I beleive this indicates that the referenced member of BuildingBlockEntries (e.g. Automatic Table 2) does not exist, but I am unclear as to why or how to correct it.

Thank you for the help

Edit - I attempted to use the filepath to the application's default Building Blocks template as suggested:

Application.Templates("C:\Program Files\Microsoft Office\Office12\Document Parts\1033\Building Blocks.dotx").BuildingBlockEntries("Automatic Table 2").Insert Where:=Selection.Range _ , RichText:=True

However, I still receive the error: Run-time error 5941 The requested member of the collection does not exist

Upvotes: 2

Views: 3807

Answers (1)

Christina
Christina

Reputation: 1379

Your code expects the Building Blocks to be found in the attached template, which, if you haven't done anything special, is probably Normal.dotm. Microsoft actually stores built-in building blocks in a different template. If you record a macro, you'll see where this template is located (mine is in "C:\Users\owner\AppData\Roaming\Microsoft\Document Building Blocks\1033\14\Built-In Building Blocks.dotx").

So, you have two options. You can use the Templates collection to get to that template and insert the building block from there (macro recorder is your friend here). Or, you can save the building block to Normal.dotm to make accessing it a little easier. To do this, click on Insert > Quick Text > Buliding Blocks, find your Building Block in the list, edit its properties, and save it to Normal. If you do that, your code should work (I have 2010, but I'm betting this is pretty similar).

I don't know of any real difference between those two options, assuming this is just for you and not something you need to distribute.

Edited to add the code I get from the macro recorder:

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

So, you should try replacing the code in InsertStyledTOC with that.

Upvotes: 1

Related Questions