0

I am attemping to access members of a template's BuildingBlockEntries collection through a macro in Microsoft Word 2007. As it is a collection, I first thought a For Each loop would be the best way to to this:

 For Each bBlock In NormalTemplate.BuildingBlockEntries
        MessageBox.Show (bBlock.Name)
    Next bBlock

However this attempt through the error: Object doesn't support property or method. So I tried this method which was suggested here:

Templates.LoadBuildingBlocks   
Dim iBB As Integer
iBB = NormalTemplate.BuildingBlockEntries.Count()
Dim bb As Word.BuildingBlock
Dim i As Integer
Dim objCounter As Object

If iBB > 0 Then

  For i = 1 To iBB
    objCounter = i
    bb = NormalTemplate.BuildingBlockEntries.Item(objCounter)
    MessageBox.Show (bb.Name)
  Next

End If   

However, this is resulting in the error shown in the title: Object variable or With Block variable not set.

I have tried just using an integer variable for the index, i specifically, but with now avail. How can I acheive the desired effect? What is wrong with my attempt?

Thank you for the help.

R3uK
  • 14,417
  • 7
  • 43
  • 77
Christian
  • 1,685
  • 9
  • 28
  • 48

1 Answers1

1

With your 2nd question the answer is that you need to use Set, as bb is an object:

Set bb = NormalTemplate.BuildingBlockEntries.Item(objCounter)

For more info on Set take a look at this SO question.

With your For/Next loop, it's not clear how you've declared bBlock. I guess it should be something like:

Dim bBlock as BuildingBlock

And perhaps the For line should reference BuildingBlocks instead of BuildingBlockEntries:

For Each bBlock In NormalTemplate.BuildingBlocks

I don't know for sure though, as I'm just looking at what pops up in Intellisense.

Community
  • 1
  • 1
Doug Glancy
  • 27,214
  • 6
  • 67
  • 115
  • If I use `Set` keyword for bb, it exposes that a `Set` call is also required for objCounter, which still results in a type mismatch error when I call: `Set bb = NormalTemplate.BuildingBlockEntries.Item(objCounter)` Likely because `BuildingBlockEntries.Item()` is looking for an integer as an argument. However, if I remove `objCounter = i ` and change the argument of the call to: `Set bb = NormalTemplate.BuildingBlockEntries.Item(objCounter)` I get the desired effect. Thanks for the info. Still unclear on how to get at the template with the basic Building Blocks, but that's a different question. – Christian Mar 01 '13 at 18:58