Solved

Why won't this building block macro run?

Posted on 2014-11-06
6
301 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
Don't Miss ATEN at InfoComm 2017!

Visit booth #2167 to see the  new ATEN VM3200 32 x 32 Modular Matrix Switch. Other highlights include the VE8950 4K HDMI Over IP Extender, VS1912 12-Port DP Video Wall Media Player  and VK2100 ATEN Control System. Register now with Free Pass Code ATEN288!

 
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

Save the day with this special offer from ATEN!

Save 30% on the CV211 using promo code EXPERTS30 now through April 30th. 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.

Question has a verified solution.

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

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.
Today companies are subjected to more-and-more data, and it won't stop any time soon.  But there are obvious opportunities for reducing data, particularly data duplicated among companies.
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

717 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