Bryce Bassett
asked on
Trouble inserting building blocks programmatically using VBA in MS Word
I'm developing a Word 2010 template with some content library functions. I simply want to use VBA to insert a building block from a template other than the built-in building blocks template. I've used this method successfully in the past but am having a real problem. Sometimes it works fine, but other times it chokes on "Set bbTmp = Templates(bbpath)" and tells me "the requested member of the collection does not exist." I've spent hours and cannot identify a pattern for why it sometimes sets the template correctly but other times does not work.
Any help would be appreciated!
Thanks.
dim bbpath as string
dim bbTmp as template
bbpath = contentlibraryaddress & "\Product_Descriptions.dotx"
Set bbTmp = Templates(bbpath)
bbTmp.BuildingBlockTypes(wdTypeQuickParts).Categories("Technical Information") _
.BuildingBlocks("Test").Insert Selection.Range
Any help would be appreciated!
Thanks.
ASKER
Thanks,
I want to use this like a global template, i.e. an external custom building blocks source that can be deployed in a common folder on a network for multiple users.
On opening a document, this is not by default a member of the Templates collection. I realize I need to make it so before I can pull bb's from it. I presume that is what the "set bbTmp = Templates (bbpath)" is accomplishing, but I can't find documentation on that function. I got that method from Greg Maxey's site and have used it successfully before.
As an alternate, I tried to add it to the Templates collection using set bbTmp = Application.Addins.Add (bbpath), which seems to work because I can now see it when I iterate through my Templates collection. But then when I try to pull a bb from it using that last line of code, I can't seem to address it.
Am I missing something?
I want to use this like a global template, i.e. an external custom building blocks source that can be deployed in a common folder on a network for multiple users.
On opening a document, this is not by default a member of the Templates collection. I realize I need to make it so before I can pull bb's from it. I presume that is what the "set bbTmp = Templates (bbpath)" is accomplishing, but I can't find documentation on that function. I got that method from Greg Maxey's site and have used it successfully before.
As an alternate, I tried to add it to the Templates collection using set bbTmp = Application.Addins.Add (bbpath), which seems to work because I can now see it when I iterate through my Templates collection. But then when I try to pull a bb from it using that last line of code, I can't seem to address it.
Am I missing something?
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
I understand putting it in Startup, or in the Document Building Blocks folder of appdata is the preferred method, but I'm trying to avoid my client having to copy the building blocks template around to each individual machine each time it is updated. That's why I'd like it to live on a common folder all can access.
Thanks for helping me think through this. Here's what I've come up with and it seems to be working consistently. This relies on the fact that the newest attached template becomes template (1):
Thanks for helping me think through this. Here's what I've come up with and it seems to be working consistently. This relies on the fact that the newest attached template becomes template (1):
bbpath = contentlibraryaddress & "\Product_Descriptions.dotx"
Application.AddIns.Add (bbpath)
Set bbTmp = Application.Templates(1)
bbTmp.BuildingBlockTypes(wdTypeQuickParts).Categories("Technical Information") _
.BuildingBlocks(Att3.Caption).Insert Selection.Range
I think I'm in business. Thanks again
Is it the attached template or a global template?