Home > OS >  How can I see the NewEnum method that for each calls?
How can I see the NewEnum method that for each calls?

Time:08-22

In the microsoft docs for 'collection', it says that

Collections must implement a method called NewEnum that accepts no arguments, returns an appropriate IUnknown object, and has its VB_UserMemId attribute set to -4.

If I look at 'Collection' in the excel VBA object browser, I can only see Add, Count, Item and Remove.

How do I see that Collection has a NewEnum method? Where do I see documentation for its NewEnum method? Is there any way to see the source code?

Many thanks!

CodePudding user response:

Collections are Standard Objects defined in the COM Automation standard. This was written at the same time as VBA and for VBA. A programmer has to implement the collection.

A collection is created by a program according to some rules. There is no source code as it a standard that programmers must follow to create a collection. It purpose is to enable the For - Each construct.

VBA has an implementation of a generic collection for programmers to quickly use but advise not to use it in code that other code calls. You are supposed wrap VBA's generic collection in a class. You can stick anything in VBA's generic collection and you write a class to restrict what can be added.

When you get a collection from Excel Excel works out what to give you by what object you asked for in your For Each loop. EG Rows inherit from range so you treat it as a range object as it has same properties and methods. Under COM rules it could have more methods but not fewer than the range object. If you ask a rows object, via IUnknown:QueryInterface, are you a range it will answer yes.

From https://docs.microsoft.com/en-us/windows/win32/api/oaidl/nn-oaidl-ienumvariant

Collection Object Properties

A collection provides a set of objects over which iteration can be performed. All collection objects must provide the following properties:

Property name Return type Description

Count  VT_I4 Returns the number of items in the collection; read only. Required. 

_NewEnum  VT_DISPATCH A special property that returns an enumerator object that 
implements IEnumVARIANT. Required. 

IEnumVARIANT Interface

The IEnumVARIANT interface provides a method for enumerating a collection of variants, including heterogeneous collections of objects and intrinsic types. Callers of this interface do not need to know the specific type (or types) of the elements in the collection.

Implemented by Used by Header file name

Applications that expose collections of objects Applications that access collections of objects Oleauto.h (32-bit systems)

Dispatch.h (16-bit systems)

The following is the definition that results from expanding the parameterized type IEnumVARIANT:

interface IEnumVARIANT : IUnknown { 
   virtual HRESULT Next(unsigned long celt, 
               VARIANT FAR* rgvar, 
               unsigned long FAR* pceltFetched) = 0;
   virtual HRESULT Skip(unsigned long celt) = 0;
   virtual HRESULT Reset() = 0;
   virtual HRESULT Clone(IEnumVARIANT FAR* FAR* ppenum) = 0;
   };

To see how to implement a collection of objects using IEnumVARIANT, refer to the file Enumvar.cpp in the Lines sample code.

IEnumVARIANT Methods Description

Clone Creates a copy of the current state of enumeration. 
Next Gets the next items in the enumeration sequence 
Reset Resets the enumeration sequence to the beginning. 
Skip Attempts to skip over the next celt elements in the enumeration sequence.
  •  Tags:  
  • vba
  • Related