I tried to add one VBA project Lib
as reference for a second VBA project App
and the default class members don't work.
Is this a limitation of VBA? Or is there something I need to do to get it to work?
Here are some more details:
- In
Lib
I have defined the classMyClass
with the default memberDefaultMember
(defined by addingAttribute MyClass.VB_UserMemId = 0
with a text editor to the module). - I added
Lib
as a reference to theApp
- I can
Dim X As MyClass
in bothLib
andApp
. - In
Lib
these are equivalent:X.DefaultMethod(123)
andX(123)
. - In
App
this works:X.DefaultMethod(123)
, but this doesn't:X(123)
.
EDIT
Turns out I had a conflict with class names and everything works as expected: some of the names of my classes with default members were also used in other referenced libraries. Perhaps after adding the new Lib
as a reference, the default MyClass
all of a sudden changed and became the one defined in another library that doesn't have the default member.
Attribute Table.VB_UserMemId = 0 is Incorrect.
It should be
Attribute DefaultMember.VB_UserMemId = 0
And the attribute line should be located in the DefaultMember method.
If you plan on doing more of this I'd install the fantastic RubberDuck addin for VBA and use the '@DefaultMember annotation.
Update
Here is an example of a references to an external library (VBA in Word Templates) that are working fine.
The code
Placed in the Normal module executes correctly to give the output
A random comment ->HelloThereWorld
What is not shown above is the 'Set c.cvt = Convertorproj.convertor' that live in the initialisation code for the Stro object and the stro object has a default member of .Value.