-1

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.

stenci
  • 8,290
  • 14
  • 64
  • 104

1 Answers1

2

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.

freeflow
  • 4,129
  • 3
  • 10
  • 18
  • My question is about using one project as external reference. Thanks for pointing out the copy/paste error with the name of the class. I fixed that. – stenci Sep 30 '20 at 17:43
  • I tried the RubberDuck addin long time ago, but it was unusable: it was working only with very small projects, which is when you don't need it. My VBA projects are usually more than 30,000 lines, and the RubberDuck addin was chocking. Still, it doesn't solve the problem I'm asking about – stenci Sep 30 '20 at 17:46
  • @stenci - 30,000 lines, that's the real problem. – BigBen Sep 30 '20 at 18:03
  • What happens if you dim X as Lib.Myclass. VBA may not be able to correctly dereference a X() especially if you don't use Option Explicit. – freeflow Sep 30 '20 at 18:04
  • @freeflow same behavior. The name of the library is required only for disambiguation and can be omitted. – stenci Sep 30 '20 at 19:02
  • @stenci I added an example of some of my code using external vba libs – freeflow Sep 30 '20 at 20:59
  • @freeflow I read your answer a few times, but I don't understand what's going on or if it even tries to answer my question. Are you saying that the default method works on both Lib and App? What is the difference between your implementation and mine? – stenci Sep 30 '20 at 21:14
  • @freeflow I don't think you can upload files here. I started a chat room, it's the first time, I don't know if it works. Can you try to go here: https://chat.stackoverflow.com/rooms/222330/room-for-stenci-and-freeflow ? – stenci Sep 30 '20 at 22:08