What do you do if you need to enumerate an Office collection using late binding and when you call Item it does not work because it's not expecting an index?

What do you do if you need to enumerate an Office collection using late binding and when you call Item it does not work because it's not expecting an index?

Well, in that case you need to use the IEnumVariant collection, right?
Not so fast, in late binding that is NOT quite easy because you need to connect an early binding thing (the IEnumVariant interface) to a late binding thing, your collection. Sounds impossible doesn't it? Well, it turns out that if you know Delphi really well, there is a way to do it :)

When you use late binding, you get hold of your COM object using an OleVariant variable, right? What you may not know, however, is that an OleVariant can hold values of type IUnknown.

Office collections can be enumerated via a _NewEnum call which returns an IUnknown. This does the trick. You assign the result of the _NewEnum call to an OleVariant, then this gets assigned to a variable of type IUnknown, which in turn gets assigned to a variable of type IEnumVariant.

At this point you got hold of a way to do the enumeration  :)

Remember to nil the interfaces when you are done :)

A

Comments