Solved

Why won't this building block macro run?

Posted on 2014-11-06
6
287 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
PaperPort has a feature called the "Send To Bar". It provides a convenient, drag-and-drop interface for using other installed software, such as Microsoft Office. However, this article shows that the latest Office 2016 apps (installed with an Office …
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

803 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