Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Recorded macro: The requested member of the collection does not exist 1

Status
Not open for further replies.

JustinEzequiel

Programmer
Jul 30, 2001
1,192
PH
Using MS Word 2007, I recorded a macro to insert a table of contents.
Resulting code below.
Code:
    ActiveDocument.AttachedTemplate.BuildingBlockEntries("Automatic Table 1"). _
        Insert Where:=Selection.Range, RichText:=True
When running the macro on the same document I used to record the macro, I get
Run-time error '5941':
The requested member of the collection does not exist.
What template must I attach to get this to work?
 

The Building Blocks mechanism seems to have some bits missing (but maybe I just haven't found them yet), and is a bit clumsy.

First of all, you have to know where the BB is, and you have to have the right BB File open. If it's in a BB Template, Word will not usually open it until it is used so, in VBA, you must explicitly ensure it is open with ...

[tt] Templates.LoadBuildingBlocks[/tt]

Next you must explicitly specify the BB file - and what is recorded is usually not where your BB is. Most likely, given the name, it is in the default BB Template - "Building Blocks.dotx" - but you still need to know the path. You can usually find this by iterating over the templates collection but if you have a complex set up it may be more difficult. Once you have the full name you're good to go! In a simple case, this should work ...

Code:
[blue]Templates.LoadBuildingBlocks
For Each Template In Templates
    If Template.Name = "Building Blocks.dotx" Then
        Templates(Template.FullName). _
            BuildingBlockEntries("Automatic Table 1"). _
            Insert Where:=Selection.Range, RichText:=True
        Exit For
    End If
Next[/blue]


Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top