Take NO for an answer. Cannot return instance name as string literal in VBA.
I still donโt understand the reason why you can do this ... Anyway
The easiest way to find out the name of each instance is to create a property for the class in which the actual name is stored. This will result in the name being only as a String property, and not the actual reference to the object - it already has a link - itself!
So create a class module
Class1
Option Explicit Public MyName as String
and in Module1 all that is required is
Option Explicit Sub Main() Dim c As Class1 Set c = New Class1 c.MyName = "c" Debug.Print c.MyName End Sub
And there you go :)
Another way is to create a dictionary to store KEY / VALUE pairs.
Sub Main() Dim c As Class1 Set c = New Class1 Dim dict As Object Set dict = CreateObject("Scripting.Dictionary") dict.Add "c", c Debug.Print dict.Exists("c") End Sub
Now you can do what you want, but that would be very ugly. This is how I am not going to demonstrate.
You would create an instance of a custom class. Using ObjPtr, you can get this link in memory. Then you need a mechanism that scans your module code line by line and finds the names of all the variables that you defined. After you get a list of all the variables, you will need a mechanism that tries to create an instance of the same type (class). Once you get past this point, you can try myNewObj = c ("c" is an obj instance) programmatically. If this succeeds, you must do ObjPt for both and match your addresses in memory - you will get a match that you know, the name of the variable. Grree, please do not do it like this: P
user2140173
source share