Solved

Why won't this building block macro run?

Posted on 2014-11-06
6
290 Views
Last Modified: 2014-11-18
The following macro fails to run.  It was created using Word 2007 macro recorder.  I can perform these steps manually but when using the macro, it fails at the ActiveDocument statement.  Why?  See attached document.

Sub Test()
'
' Test Macro
'
    Selection.HomeKey Unit:=wdStory
    ActiveDocument.AttachedTemplate.BuildingBlockEntries("Meeting Cancelled"). _
        Insert Where:=Selection.Range, RichText:=True
End Sub
Test.docx
0
Comment
Question by:cowboywm
  • 3
  • 2
6 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 40426660
If you get an error message, please tell us what the message is.

When I run your macro I get the message:
Run-time error '5941':
The requested member of the collection does not exist.

This is not surprising, because I don't have a Building Block Entry of that name. Have you?
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 40428368
If you are run the macro in the same document that you are trying to, please change "ActiveDocument" to ThisDocument" object.
Sometimes, if you don't have "focus" on desired document, ActiveDocument fails.
Cheers
0
 

Author Comment

by:cowboywm
ID: 40450562
This macro continues to give me an error whether I use the ThisDocument or ActiveDocument object.  I see the building block entry in the list and can insert it manually but it won't work as recorded by the macro recorder.  What gives?  See attached document.
Test.dotx
0
Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

 
LVL 76

Expert Comment

by:GrahamSkan
ID: 40450698
We can't reliably diagnose the error because we don't have the template that is Attached to your document. Also you haven't told us what the error message is.
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 40451097
I think I see the problem. The Macro recorder isn't guaranteed to run under all circumstances.

In this case, we cannot be sure that the built-in Building Blocks template has been loaded. Nor is it likely to be the Attached template

This code will ensure that it is loaded. There is also a function to identify it positively by name.
Sub Test()
    Templates.LoadBuildingBlocks
    GetBBTemplate.BuildingBlockEntries("Meeting Cancelled"). _
        Insert Where:=ActiveDocument.Range(0, 0), RichText:=True
End Sub
Function GetBBTemplate() As Template
    Dim tmpl As Template
    
    For Each tmpl In Templates
        If tmpl.Name = "Building Blocks.dotx" Then
            Set GetBBTemplate = tmpl
            Exit Function
        End If
    Next tmpl
End Function

Open in new window

0
 

Author Closing Comment

by:cowboywm
ID: 40451132
For some reason the building block template got disconnected.  I have repaired the file and it works as designed.  Thank you all for your insight.

Bill
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lack of Storage capacity is a common problem that exists in every field of life. Here we are taking the case of Lotus Notes Emails, as we all know that we are totally depend on e-communication i.e. Emails. This article is fully dedicated to resolvin…
Notes Document Link used by IBM Notes is a link file which aids in the sharing of links to documents in email and webpages. The posts describe the importance and steps to create a Lotus Notes NDL file in brief.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
An overview on how to enroll an hourly employee into the employee database and how to give them access into the clock in terminal.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question