PDA

View Full Version : Solved: BuildingBlock Insertion



mphill
09-23-2008, 11:14 AM
Hello,

I am trying to insert a carriage return then a buildingblock (quickpart) after a specific word "page" in the text, programmatically. An error appears at the NormalTemplate line. I have tried various scenarios and have resorted to just trying to get the default buildingblock Matrix in.

I have tried calling the building block.dotx, too, without any success.
Any suggestion is greatly appreciated.


Sub insertBldgBlkAfterPage()
Dim r As Range

Set r = ActiveDocument.Range
With r.Find
Do While .Execute(FindText:="Page", _
Forward:=True) = True
r.InsertAfter (Chr(13))
NormalTemplate.BuildingBlockTypes(wdTypeQuickParts).Categories( _
"Built-In").BuildingBlocks("Matrix").Insert Where:=Selection.Range
r.Collapse 0
Loop
End With
End Sub

TonyJollans
09-23-2008, 11:59 AM
I'm not sure that the macro will do what you want, but it works for me. Are you sure you have the specified building block in Normal?

mphill
09-23-2008, 12:05 PM
I am trying to access the default dotx located in C:\Program Files\Microsoft Office\Office12\Document Parts\1033\building blocks.dotx in my Wy_Stake_Rpt.dotm. The list of blocks appear in the quickparts in my dotm.

mphill
09-23-2008, 04:06 PM
Okay, I have the vbCR in the correct spots now but the normaltemplate line still returns error 5941, requested member of the collection does not exist.

I must be missing a tie-in somewhere. The dotm is attached.

Sub insertBldgBlkAfterPage()
Dim r As Range
Set r = ActiveDocument.Range

With r.Find
Do While .Execute(FindText:="Page", _
Forward:=True) = True
r.MoveEnd unit:=wdParagraph
' r.InsertAfter (Chr(13))
r.InsertAfter txbProject.Text & vbCr
r.InsertAfter txbDescription.Text & vbCr
NormalTemplate.BuildingBlockTypes(wdTypeQuickParts).Categories( _
"Built-In").BuildingBlocks("Matrix").Insert Where:=Selection.Range
r.Collapse 0
Loop
End With
End Sub

TonyJollans
09-24-2008, 02:55 AM
I am trying to access the default dotx located in C:\Program Files\Microsoft Office\Office12\Document Parts\1033\building blocks.dotx in my Wy_Stake_Rpt.dotm. The list of blocks appear in the quickparts in my dotm.

I'm afraid you've lost me here - what template is your building block in? BuildingBlocks.dotx or Wy_Stake_Rpt.dotm? Whichever it is, you don't mention Normal here so I'm guessing it isn't in Normal, which would explain why your code can't find it in Normal.

mphill
09-24-2008, 06:31 AM
I am in the wy_stake_rpt.dotm, not the normal. The Word class I just attended said all of the buildingblocks are stored in the buildingblock.dotx. Since that appears as the default dotx I was hoping it would be attaching by default, too. I don't know what is stored in the normal.dotm.
Should I have copied the normal.dotm and renamed it then placed the code in?
I have tried about a dozen different methods of getting this block in, from using the examples in help, msdn, and various news groups. Just seems like the same error appears no matter which procedure I have tried. I am still working on it.
Thanks for the information and help.

TonyJollans
09-24-2008, 08:49 AM
Building blocks supplied with Word are in BuildingBlocks.dotx.
Building blocks that you create yourself can be in any Template.

Your code (NormalTemplate.BuildingB... ) is trying to find a Building Block in Normal, instead of looking wherever you have stored your building block (try Templates("Wy_Stake_Rpt.dotm").BuildingB... instead).

mphill
09-24-2008, 09:27 AM
That returned the same error. I also tried in normal.dotm. I have also tried copying and renaming the normal.dotm.
A recorded macro returns (failed):
' ActiveDocument.AttachedTemplate.BuildingBlockEntries( _
' "Matrix").Insert Where:=Selection.Range, _
' RichText:=True

another attempt from MSDN to get anything in:
'' Dim objTemplate As Template
'' Dim objBB As BuildingBlock
''
'' Set objTemplate = Templates(1)
'' Set objBB = objTemplate.BuildingBlockEntries(1)
'' objBB.Insert where:=Selection.Range

still another:
''' Templates("C:\Program Files\Microsoft Office\Office12\Document _ Parts\1033\building blocks.dotx").BuildingBlockTypes(wdTypeQuickParts).Categories( _
"Built-In").BuildingBlocks("Matrix").Insert Where:=Selection.Range

After eleventeen (just made that up :cool: ) tries and two days I am going to try placing the information in another way.

TonyJollans
09-24-2008, 10:27 AM
Two questions ...

How did you create the building block?

What is shown in the building block's property dialog? (select the BB in the BB organiser and press "Edit Properties)

mphill
09-24-2008, 10:40 AM
The Matrix is a default BB from Microsoft. I had hoped if one of these came in any others could be called.
Name: Matrix
Gallery: Tables
Category: Built-in
Description: Evenly spaced...
Save in: Building Blocks.dotx
Options: Insert content in its own paragraph.

I have found a way to insert the text by calling a function. It places the text in but does not iterate the insertion point where I need it, all of the text ends up on the first page. This may be the way to go. The original issue is not resolved but this function has gotten me almost to the completion of the project. And "they" need it yesterday, of course. LOL

TonyJollans
09-24-2008, 11:41 AM
Matrix is in the "Tables" Gallery, not "Quick Parts", so this should work better ...

Templates("C:\Program Files\Microsoft Office\Office12\Document _ Parts\1033\building blocks.dotx").BuildingBlockTypes(wdTypeTables).Categories( _
"Built-In").BuildingBlocks("Matrix").Insert ...

TonyJollans
09-24-2008, 11:47 AM
I noticed the line continuation in the middle of the (cut and pasted) line after posting and was about to edit it when I realised it was the wrong path. You should use ...

Templates("C:\Users\(userid)\AppData\Roaming\Microsoft\" & _
"Document Building Blocks\1033\Building Blocks.dotx"). _
BuildingBlockTypes(wdTypeTables). _
Categories("Built-In"). _
BuildingBlocks("Matrix").Insert Where:=Selection.Range

mphill
09-25-2008, 07:02 AM
Thanks for the information. I will be using this on future projects.