Looping through the BuildingBlocks collection

T

TFTAJLLYMXZP

I'm trying to convert some macro code from our Word 2003 templates to
work with Word 2007, but am confused by the behaviour of the
BuildingBlocks collection. Why is it that I cannot loop through the
collection with a For Each..Next structure? The following fragment
throws error #438 when it reaches the "For Each..." line, stating
"Object doesn't support this property or method."

Sub TestThis
Dim objTemplate As Template
Dim objBB As BuildingBlock

Set objTemplate = ActiveDocument.AttachedTemplate

For Each objBB In objTemplate.BuildingBlockEntries
<do stuff>
Next objBB

Any help will be greatly appreciated,

Alder
 
G

Greg Maxey

You are right. Despite that even the VBA intellisense leads one to believe
the that for each method should work the errors you mention do occur.
Instead of using for each try For X = 1 to BB.Count.

Sub ListAllBuildingBlocks()
Dim oBBT As BuildingBlockType
Dim oCat As Category
Dim oRng As Word.Range
Dim oBB As BuildingBlock
Dim h As Long
Dim i As Long
Dim j As Long
Dim k As Long
Set oRng = ActiveDocument.Range
For h = 1 To Templates.Count
Set oTmp = Templates(h)
For i = 1 To oTmp.BuildingBlockTypes.Count
Set oBBT = oTmp.BuildingBlockTypes(i)
If oBBT.Categories.Count > 0 Then
'Categories can exist that don't have any BBEs
If ValidateCategories(oBBT) = True Then
oRng.InsertAfter oTmp.Name & "/" & "Building Block Type: " &
oBBT.Name + vbCr
oRng.Paragraphs.Last.Previous.Format.Shading. _
BackgroundPatternColor = wdColorGray25
For j = 1 To oBBT.Categories.Count
Set oCat = oBBT.Categories(j)
If oCat.BuildingBlocks.Count > 0 Then
oRng.InsertAfter "Buidling Block Category: " & oCat.Name + vbCr
oRng.Paragraphs.Last.Previous.Format.Shading. _
BackgroundPatternColor = wdColorGray10
For k = 1 To oCat.BuildingBlocks.Count
oRng.InsertAfter oBBT.Categories(j).BuildingBlocks(k).Name +
vbCr
Next
End If
Next
End If
End If
Next
Next
End Sub
Function ValidateCategories(ByRef BBT As BuildingBlockType) As Boolean
Dim i As Long
For i = 1 To BBT.Categories.Count
If BBT.Categories(i).BuildingBlocks.Count > 0 Then
ValidateCategories = True
Exit Function
End If
Next
End Function
 
T

TFTAJLLYMXZP

Thanks for confirming this, Greg. Are there any other new collections
that behave like this, to your knowledge?
 

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