Building Blocks Macro Error

J

Jean D

Regarding Word 2007 macros --

I ran across a post that said if your macro crashes on the line:
ActiveDocument.AttachedTemplate.BuildingBlockEntries("Plain Number 1"). _
Insert Where:=Selection.Range, RichText:=True

Then move the entry to the normal template and use:
NormalTemplate.BuildingBlockEntries("Plain Number 1").Insert _
Where:=Selection.Range, RichText:=True

While this works perfectly for one entry, is there a way to do it
differently to avoid moving numerous built-in building block entries from
the building blocks template to normal?

I saw an example for something else with the syntax:
Set objTemplate = Templates("C:\Documents and Settings\" _
& "[userid]\Application Data\Microsoft\Document Building" _
& "Blocks\1033\Building Blocks.dotx")
Set objBB=objTemplate.BuildingBlockTypes(wdTypeAutoText). _
Categories("Receipts").BuildingBlocks("Receipt")
objBB.Insert Selection.Range

Is there a way to use similar syntax to access the built-in entries from
building blocks.dotx?

Thanks!
 
T

Tony Jollans

Yes is the answer to your question, but

(a) I don't follow why you need to - what error do you get that is resolved
by this process?

(b) You twice mention Building Blocks.dotx and built-in building blocks, but
your code references a different template. Where are the building blocks you
are having trouble with?
 
J

Jean D

I apologize! I should have been more clear.

In any document in Word 2007, run the following:

Sub Test()
'Insert the "Plain Number 2" style page number
ActiveDocument.AttachedTemplate.BuildingBlockEntries("Plain Number 2"). _
Insert Where:=Selection.Range, RichText:=True
End Sub

This can be a recorded macro or written in VBA. Either way, you will get
an error that states "The requested member of the collection does not
exist." That is the error I am trying to resolve.

The only resolution I have found is to use the building blocks organizer to
move "Plain Number 2" from Building Blocks.dotx to Normal.dotm and then
use:

Sub Test()
'Insert the "Plain Number 2" style page number
NormalTemplate.BuildingBlockEntries("Plain Number 2"). _
Insert Where:=Selection.Range, RichText:=True
End Sub

This works, but I would like to avoid moving all of the built-in building
blocks to the normal template.

The example code I gave later in the post was in the hopes that I could use
something like that to successfully access the building blocks from their
current location in Building Blocks.dotx.

Thank you!
 
T

Tony Jollans

Thank you for the further explanation; it tends to confirm what I suspected,
that the reason for your error is that building blocks have not been loaded
but that invoking the organizer forces them to load. I think you will find
that if, after the failure, you simply invoke the organizer - and copy
nothing - the code will then run successfully.

In order to speed up initial loading of Word, Building Block templates are
not loaded until needed. If you need them you must explicitly load them
yourself. Try adding an extra line to your macro:

Sub Test()
Templates.LoadBuildingBlocks
'Insert the "Plain Number 2" style page number
ActiveDocument.AttachedTemplate.BuildingBlockEntries("Plain Number 2"). _
Insert Where:=Selection.Range, RichText:=True
End Sub
 
J

Jean D

The load does add the Building Blocks template to the project explorer in
the VB editor which looked quite promising; however, then I still get the
error. I added the project as a reference to see if the location looked
correct and from what I could see, it did ("c:\documents and settings\
[userid]\application data\microsoft\document building blocks\1033\Building
Blocks.dotx"). I'm sorry to report that I'm still stuck.

Thank you for your help!

Connie
 
T

Tony Jollans

I think you are looking in the wrong template for the building block. You
say you copy it (manually) from Building Blocks.dotx to make it work but
your code is looking in the template for the active document.

Instead of:

ActiveDocument.AttachedTemplate. _
BuildingBlockEntries("Plain Number 1"). _
Insert Where:=Selection.Range, RichText:=True

Try using:

Templates("C:\Documents and Settings\" _
& "[userid]\Application Data\Microsoft\Document Building" _
& "Blocks\1033\Building Blocks.dotx"). _
BuildingBlockEntries("Plain Number 1"). _
Insert Where:=Selection.Range, RichText:=True

--
Enjoy,
Tony

www.WordArticles.com

Jean D said:
The load does add the Building Blocks template to the project explorer in
the VB editor which looked quite promising; however, then I still get the
error. I added the project as a reference to see if the location looked
correct and from what I could see, it did ("c:\documents and settings\
[userid]\application data\microsoft\document building blocks\1033\Building
Blocks.dotx"). I'm sorry to report that I'm still stuck.

Thank you for your help!

Connie



Thank you for the further explanation; it tends to confirm what I
suspected,
that the reason for your error is that building blocks have not been
loaded
but that invoking the organizer forces them to load. I think you will
find
that if, after the failure, you simply invoke the organizer - and copy
nothing - the code will then run successfully.

In order to speed up initial loading of Word, Building Block templates
are
not loaded until needed. If you need them you must explicitly load them
yourself. Try adding an extra line to your macro:

Sub Test()
Templates.LoadBuildingBlocks
'Insert the "Plain Number 2" style page number
ActiveDocument.AttachedTemplate.BuildingBlockEntries("Plain Number 2"). _
Insert Where:=Selection.Range, RichText:=True
End Sub
 
J

Jean D

I had tried that too, thinking that the activedocument code was wrong. No
go. I still get that the collection member is not found. Weird. I'm
still at the point where the only way it works is to move the entry to
normal.dot. I hate that!

Thanks!
 
T

Tony Jollans

I just cut and pasted the code I posted and I see that there should be a
space between "Building" at the end of the second line and "Blocks" at the
start of the third line. Is that perhaps the problem? If not, are your
building blocks in that directory? Or are you using a different language
from US English (I don't know where in the world you are).

--
Enjoy,
Tony

www.WordArticles.com

Jean D said:
I had tried that too, thinking that the activedocument code was wrong. No
go. I still get that the collection member is not found. Weird. I'm
still at the point where the only way it works is to move the entry to
normal.dot. I hate that!

Thanks!


I think you are looking in the wrong template for the building block. You
say you copy it (manually) from Building Blocks.dotx to make it work but
your code is looking in the template for the active document.

Instead of:

ActiveDocument.AttachedTemplate. _
BuildingBlockEntries("Plain Number 1"). _
Insert Where:=Selection.Range, RichText:=True

Try using:

Templates("C:\Documents and Settings\" _
& "[userid]\Application Data\Microsoft\Document Building" _
& "Blocks\1033\Building Blocks.dotx"). _
BuildingBlockEntries("Plain Number 1"). _
Insert Where:=Selection.Range, RichText:=True
 
J

Jean D

Good thought, but it still fails with the same error. I'm in Oregon by the
way. So here is the question: Does the code (as corrected) work for you?
This is a built-in template and a built-in building block entry so it seems
like the problem should be reproducible.

Thanks!
 
T

Tony Jollans

Yes. It works for me. I had to change the path because I'm running on Vista,
but otherwise it works as is.

Rather than typing in a complete path, here is some code that loops through
the templates to find "Building Blocks.dotx" and then uses the path from
there. This is cut and pasted from my VBE and is exactly what works for me.

Sub BBTest()

Dim T As Template

Templates.LoadBuildingBlocks

For Each T In Templates
If T.Name = "Building Blocks.dotx" Then Exit For
Next

Templates(T.FullName).BuildingBlockEntries("Plain Number 1"). _
Insert Where:=Selection.Range, RichText:=True

End Sub
 
J

Jean D

Tony, this works perfectly! I just looked back at our thread to see why
this would work when the identical path didn't work, and found I owe you an
apology. In the later posts where the ActiveDocument.AttachedTemplate was
changed to Templates([name]), I failed to carry over the building blocks
load from the earlier email. I owe you time back in your life! Thank you
so much for your help and infinite patience. This will make my life much
easier.

Jean
 
J

Jean D

Tony, this works perfectly! I just looked back at our thread to see why
this would work when the identical path didn't work, and found I owe you an
apology. In the later posts where the ActiveDocument.AttachedTemplate was
changed to Templates([name]), I failed to carry over the building blocks
load from the earlier email. I owe you time back in your life! I must say
though that it was not wasted effort because with this code I will not have
to address the issue of a particular userID.

Thank you so much for your help and infinite patience. This will make my
life much easier.

Jean
 
T

Tony Jollans

I'm so glad you're sorted. Building Blocks can be awkward to work with in
code.

My time was well used; you owe me nothing :)

Just so that you know, this last code - that loops through the templates
collection - is not foolproof, as you could have more than one template
called "Building Blocks.dotx" - probably not in normal circumstances, but it
is certainly possible. I just wrote it as a way of trying to avoid
transcription errors while resolving your problem.

--
Enjoy,
Tony

www.WordArticles.com

Jean D said:
Tony, this works perfectly! I just looked back at our thread to see why
this would work when the identical path didn't work, and found I owe you
an
apology. In the later posts where the ActiveDocument.AttachedTemplate was
changed to Templates([name]), I failed to carry over the building blocks
load from the earlier email. I owe you time back in your life! I must
say
though that it was not wasted effort because with this code I will not
have
to address the issue of a particular userID.

Thank you so much for your help and infinite patience. This will make my
life much easier.

Jean


Yes. It works for me. I had to change the path because I'm running on
Vista,
but otherwise it works as is.

Rather than typing in a complete path, here is some code that loops
through
the templates to find "Building Blocks.dotx" and then uses the path from
there. This is cut and pasted from my VBE and is exactly what works for
me.

Sub BBTest()

Dim T As Template

Templates.LoadBuildingBlocks

For Each T In Templates
If T.Name = "Building Blocks.dotx" Then Exit For
Next

Templates(T.FullName).BuildingBlockEntries("Plain Number 1"). _
Insert Where:=Selection.Range, RichText:=True

End Sub
 
J

Jean D

That was a great way of figuring out what I was doing wrong and I
appreciate the warning. I'll address that in my final code.

Thank you again SO much!

Jean
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top