Introduction
In VB6 and VBA, the VB_UserMemId attribute is used to assign a unique identifier to a method or property of a class module. This identifier, also known as the user-defined member ID, can be used to invoke the method or property at runtime using the IDispatch interface.
The VB_UserMemId attribute can be applied to methods and properties in a class module by including the following line before the method or property declaration:
Public Function MyMethod() As Integer
' Method code goes here
End Function
Attribute MyMethod.VB_UserMemId = 123
In this example, the MyMethod function has been assigned a UserMemId of 123. This means that the method can be invoked at runtime using the IDispatch::Invoke method, passing the value 123 as the dispidMember parameter.
It’s important to note that UserMemId and DispID are quite similar and serve as unique identifiers for methods and properties.
- DispID is a value that is automatically assigned to methods and properties by the VBA compiler
- UserMemId is a value that is assigned by the programmer using the
VB_UserMemIdattribute.
When a class module contains methods or properties with both DispID and UserMemId values, the UserMemId value takes precedence when invoking the method or property at runtime.
The table provided belows lists several DispIDs for commonly used properties and methods in VBA. These DispIDs can be used to invoke the corresponding methods and properties using the IDispatch::Invoke method. The table provides a brief description of each property and method, as well as its corresponding DispID value.
| Constant | Value | Description |
|---|---|---|
DISPID_COLLECT |
-8 | The Collect property. You use this property if the method you are calling through Invoke is an accessor function. |
DISPID_CONSTRUCTOR |
-6 | The C++ constructor function for the object. |
DISPID_DESTRUCTOR |
-7 | The C++ destructor function for the object. |
DISPID_EVALUATE |
-5 | The Evaluate method. This method is implicitly invoked when the ActiveX client encloses the arguments in square brackets. For example, the following two lines are equivalent: x.[A1:C1].value = 10 and x.Evaluate("A1:C1").value = 10 |
DISPID_NEWENUM |
-4 | The _NewEnum property. This special, restricted property is required for collection objects. It returns an enumerator object that supports IEnumVARIANT, and should have the restricted attribute specified. |
DISPID_PROPERTYPUT |
-3 | The parameter that receives the value of an assignment in a PROPERTYPUT. |
DISPID_UNKNOWN |
-1 | The value returned by IDispatch::GetIDsOfNames to indicate that a member or parameter name was not found. |
DISPID_VALUE |
0 | The default member for the object. This property or method is invoked when an ActiveX client specifies the object name without a property or method. |
Source: https://learn.microsoft.com/en-us/previous-versions/windows/desktop/automat/dispid-constants
Use-cases
Here are some examples of how the VB_UserMemId attribute can be used to define a default class member and a _NewEnum member in VBA:
Default class member:
A default class member is the method or property that is invoked when an object of the class is referenced without specifying a method or property. This is often used to provide a default behavior for the object. To define a default class member in VBA, you can assign a VB_UserMemId value of 0 to the method or property that should be the default. Here’s an example:
Public Function MyDefaultMethod() As String
' Method code goes here
End Function
Attribute MyDefaultMethod.VB_UserMemId = 0
In this example, the MyDefaultMethod function has been assigned a UserMemId of 0, which makes it the default method for the class.
_NewEnum member:
The _NewEnum member is a special method that is used to return an enumerator object for a collection class. The [_NewEnum] method should return an object that supports the IEnumVARIANT interface, which is used to enumerate the items in the collection. To define a [_NewEnum] member in VBA, you can assign a VB_UserMemId value of -4 to the method. Here’s an example:
Public Function _NewEnum() As IUnknown
' Method code goes here
End Function
Attribute _NewEnum.VB_UserMemId = -4
In this example, the _NewEnum function has been assigned a UserMemId of -4, which indicates that it is the [_NewEnum] member for the class. The function should return an object that supports the IEnumVARIANT interface.