visio - VBA TypeName Behavior Inconsitent - Stack Overflow

I'm very confused right now. I'm using VBA in Visio to do some automated charts, though I don

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.

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.

Share asked Mar 3 at 23:00 TrashmanTrashman 1,5962 gold badges20 silver badges33 bronze badges 3
  • "TypeName(theseTopDims) = "Dimension_Compound" where theseTopDims is a variant that is an array". The Typename of theseTopDims will return an arrayTypename, e.g. Variant() or Dimension_Compound(), depending on how the array is originally defined. Thus comparing against "Dimension_Compound" will never work. Instead you will need to use the Instr method to see if the string returned by Typename contains "Dimension_Compound". Also, in VBA you shoud avoid using '_' in Class names as this can cause problems when you use interfaces. – freeflow Commented Mar 4 at 10:41
  • 2 Additionally you should check if your class has a default member. If this is the case then you could be falling foul of VBA's predilection of returning the type of the default member rather than the type you provide. – freeflow Commented Mar 4 at 10:46
  • @freeflow I'll take the advice about underscores in mind in future projects. That being said, those variables were working perfectly fine. It was the none underscore ones I was having the issue with. I do have a default member, but it turns out that wasn't the issue, it was a bad property name, but not a bad place to look, I know how screwy it can get when your default member is messed up. I've gotten completely nonsensical errors in the past due to that. – Trashman Commented Mar 4 at 20:49
Add a comment  | 

1 Answer 1

Reset to default 1

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745068598a4609407.html

相关推荐

  • visio - VBA TypeName Behavior Inconsitent - Stack Overflow

    I'm very confused right now. I'm using VBA in Visio to do some automated charts, though I don

    3小时前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信