I'm very confused right now. I'm using VBA in Visio to do some automated charts, though I don't think it matters that it's Visio. I have defined multiple classes, and some of them are similar but have slightly different properties for various reasons. I have functions that can take a Variant that is one of these classes, but I need to know specifically which one of my classes is being passed to the function.
So, I used TypeName
to get the name of my class as a string and use an if
or select case
block to choose the actions based on that name.
I have a file where this works perfectly. I just reopened it today to assure myself I'm not crazy. I have a class named Dimension_Compound
and I have a statement in a function If TypeName(theseTopDims) = "Dimension_Compound"
where theseTopDims
is a variant that is an array that could be of one of my custom classes, Dimension_Base
or Dimension_Compound
or an array of Integers. and it compiles and executes correctly, no issues.
I'm working on a new macro-based diagram today. I have a similar statement, Select Case TypeName(newShape)
where newShape
is Variant that could either be my custom class, PLMShape
or a standard Visio Shape
type. And, it chokes on that line. I get the error:
Wrong Number of arguments or invalid property assignment
So, I checked the Microsoft Learn help page, which clearly states: "The required varname argument is a Variant containing any variable except a variable of a user-defined type."
So, now my question isn't why doesn't my new code work, but rather how is it possible that my old code works? I was blissfully unaware of this requirement in that previous project - yet, it worked, and when I load it today, it continues to work. I checked all my reference libraries, and I even added those libraries into my new project to see if maybe one of them added functionality to TypeName
but even with those references added, it still gives me the same error.
how is it possible that my old code works?
Your old code doesn't do anything that isn't supported. The term "User-defined type" in the documentation doesn't refer to class modules, it refers to types defined like in the snippet below:
Public Type DemoType
Message As String
End Type
Sub Demo()
Dim obj As DemoType
'The following line will give a Compile Error:
'Only user-defined types defined in public object modules can
'be coerced to or from a variant or passed to late-bound functions
Debug.Print TypeName(obj)
End Sub
This means that there is no reason a priori why this code should not work assuming you have a class module named CustomClass
, you should get "CustomClass" printed to the immediate window.
Sub SimpleDemo()
Dim obj As CustomClass
Set obj = New CustomClass
Debug.Print TypeName(obj)
End Sub
Hence, your new project is doing something different, but there is not enough information in your question at the moment to answer this part of the question.
A possible scenario would be if you have defined a Sub that doesn't take any argument named TypeName
, but that would break all uses of TypeName in your module:
Sub BrokenTypeNameDemo()
Dim obj As CustomClass
Set obj = New CustomClass
'This line will give a compile error: Wrong Number of arguments or invalid property assignment
'That's because it tries to use the definition of TypeName as defined in the private sub below.
Debug.Print TypeName(obj)
End Sub
Private Sub TypeName()
End Sub