?
Solved

Why won't this building block macro run?

Posted on 2014-11-06
6
Medium Priority
?
307 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 2000 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Sometimes clients can lose connectivity with the Lotus Notes Domino Server, but there's not always an obvious answer as to why it happens.   Read this article to follow one of the first experiences I had with Lotus Notes on a client's machine, my…
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …
Suggested Courses

762 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