Using default class members in VBA projects used as external reference

95 views Asked by At

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 class MyClass with the default member DefaultMember (defined by adding Attribute MyClass.VB_UserMemId = 0 with a text editor to the module).
  • I added Lib as a reference to the App
  • I can Dim X As MyClass in both Lib and App.
  • In Lib these are equivalent: X.DefaultMethod(123) and X(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.

1

There are 1 answers

8
freeflow On

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.

enter image description here

The code

Option Explicit

Sub Test()


    
    Dim myStro As StrO
    Set myStro = StrO.Make("A random comment ->")
    
    
    Set myStro = myStro.Merge(-1, "Hello", "There", "World")
    Debug.Print myStro
    
End Sub

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.