Building Block Loop - won't insert Building Block

J

JosephSmith

Hi need help with trying to loop a building block - it won't insert.

Building block is saved in Building Blocks.dotx for the moment as:
Custom Autotext
Category Receipts
Title Receipt

Error Highlighted:
Set objBB = objTemplate.BuildingBlockTypes(wdTypeCustomAutoText) _ *** Error Requested Member of Collection Does not Exist
.Categories("Receipts").BuildingBlocks("Receipt")


Sub TestMe1()

Dim intReceipt As Integer
Dim i As Integer
Dim objTemplate As Template
Dim objBB As BuildingBlock

intReceipt = InputBox("How many Receipts do you need?", "Number of Receipts")

Do While i < intReceipt
'Insert the Building Block Entry

' Set the template to store building block
Set objTemplate = ActiveDocument.AttachedTemplate

' Access the building block through the type and category

Set objBB = objTemplate.BuildingBlockTypes(wdTypeCustomAutoText) _ *** Error Requested Member of Collection Does not Exist
.Categories("Receipts").BuildingBlocks("Receipt")

' Insert building block in document replacing any selected text.
objBB.Insert Selection.Range


' ActiveDocument.AttachedTemplate.AutoTextEntries("Receipt").Insert _
' Where:=Selection.Range, RichText:=True
' 'Make sure the cursor is at the end of the Receipt
' Selection.EndKey Unit:=wdStory

'Increment the counter
i = i + 1
Loop

End Sub
 
G

Graham Mayor

I think you are overcomplicating things - try

Dim intReceipt As Integer
Dim i As Integer
Dim objTemplate As Template
Dim objBB As BuildingBlock

intReceipt = InputBox("How many Receipts do you need?", "Number of Receipts")

Do While i < intReceipt
Set objTemplate = ActiveDocument.AttachedTemplate
Set objBB = objTemplate.BuildingBlockEntries("Receipt")
objBB.Insert Selection.Range
i = i + 1
Loop

or use

Set objBB = objTemplate.BuildingBlockTypes(wdTypeAutoText) _
.Categories("Receipts").BuildingBlocks("Receipt")


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>



Hi need help with trying to loop a building block - it won't insert.

Building block is saved in Building Blocks.dotx for the moment as:
Custom Autotext
Category Receipts
Title Receipt

Error Highlighted:
Set objBB = objTemplate.BuildingBlockTypes(wdTypeCustomAutoText) _ *** Error Requested Member of Collection Does not Exist
.Categories("Receipts").BuildingBlocks("Receipt")


Sub TestMe1()

Dim intReceipt As Integer
Dim i As Integer
Dim objTemplate As Template
Dim objBB As BuildingBlock

intReceipt = InputBox("How many Receipts do you need?", "Number of Receipts")

Do While i < intReceipt
'Insert the Building Block Entry

' Set the template to store building block
Set objTemplate = ActiveDocument.AttachedTemplate

' Access the building block through the type and category

Set objBB = objTemplate.BuildingBlockTypes(wdTypeCustomAutoText) _ *** Error Requested Member of Collection Does not Exist
.Categories("Receipts").BuildingBlocks("Receipt")

' Insert building block in document replacing any selected text.
objBB.Insert Selection.Range


' ActiveDocument.AttachedTemplate.AutoTextEntries("Receipt").Insert _
' Where:=Selection.Range, RichText:=True
' 'Make sure the cursor is at the end of the Receipt
' Selection.EndKey Unit:=wdStory

'Increment the counter
i = i + 1
Loop

End Sub
 

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