Solved

Why won't this building block macro run?

Posted on 2014-11-06
6
280 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Lately there has been a variety of news related to U.S. employment.  Stories about worker productivity, automobile and airline unions, low employment and foreign laborers have frequented the news.  Each story has good and bad attributes we might arg…
For beginners of Lotus Notes user this is important to know about the types of files and their location supported by IBM Notes. Mostly users are unaware about how many file types are created and what their usages are. This Article is fully dedicated…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
An overview on how to enroll an hourly employee into the employee database and how to give them access into the clock in terminal.

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now