<p>How can you obtain the Type (the name as a string is sufficient) of an Object in VB6 at runtime?</p>

<p>i.e. something like:</p>

<pre><code>If Typeof(foobar) = "CommandButton" Then ...
</code></pre>

<p><strong>/EDIT:</strong> to clarify, I need to check on Dynamically Typed objects. An example:</p>

<pre><code>Dim y As Object 

Set y = CreateObject("SomeType")

Debug.Print( &lt;The type name of&gt; y)
</code></pre>

<p>Where the output would be "CommandButton"</p>

[{'answer_id': 52181, 'author': 'Daren Thomas', 'author_id': 2260, 'author_profile': 'https://Stackoverflow.com/users/2260', 'pm_score': 0, 'selected': False, 'text': '<p>This should prove difficult, since in VB6 all objects are COM (<code>IDispatch</code>) things. Thus they are only an interface.</p>\n\n<p><code>TypeOf(object) is class</code> probably only does a COM get_interface call (I forgot the exact method name, sorry).</p>\n'}, {'answer_id': 52243, 'author': 'Kris Erickson', 'author_id': 3798, 'author_profile': 'https://Stackoverflow.com/users/3798', 'pm_score': 4, 'selected': True, 'text': '<p>I think what you are looking for is TypeName rather than TypeOf.</p>\n\n<pre><code>If TypeName(foobar) = "CommandButton" Then\n   DoSomething\nEnd If\n</code></pre>\n\n<p>Edit: What do you mean Dynamic Objects?  Do you mean objects created with\nCreateObject(""), cause that should still work.</p>\n\n<p>Edit: </p>\n\n<pre><code>Private Sub Command1_Click()\n    Dim oObject As Object\n    Set oObject = CreateObject("Scripting.FileSystemObject")\n    Debug.Print "Object Type: " &amp; TypeName(oObject)\nEnd Sub\n</code></pre>\n\n<p>Outputs</p>\n\n<p><code>Object Type: FileSystemObject</code></p>\n'}, {'answer_id': 52255, 'author': 'Mike Woodhouse', 'author_id': 1060, 'author_profile': 'https://Stackoverflow.com/users/1060', 'pm_score': 2, 'selected': False, 'text': "<p>I don't have a copy of VB6 to hand, but I think you need the </p>\n\n<pre><code>Typename()\n</code></pre>\n\n<p>function... I can see it in Excel VBA, so it's probably in the same runtime. Interestingly, the help seems to suggest that it shouldn't work for a user-defined type, but that's about the only way I ever <em>do</em> use it.</p>\n\n<p>Excerpt from the help file:</p>\n\n<blockquote>\n  <p>TypeName Function</p>\n  \n  <p>Returns a String that provides information about a variable.</p>\n  \n  <p>Syntax</p>\n  \n  <p>TypeName(varname)</p>\n  \n  <p>The required varname argument is a\n  Variant containing any variable except\n  a variable of a user-defined type.</p>\n</blockquote>\n"}, {'answer_id': 156240, 'author': 'sharvell', 'author_id': 23095, 'author_profile': 'https://Stackoverflow.com/users/23095', 'pm_score': 2, 'selected': False, 'text': '<p>TypeName is what you want... Here is some example output:</p>\n\n<p>VB6 Code:</p>\n\n<pre><code>Private Sub cmdCommand1_Click()\nDim a As Variant\nDim b As Variant\nDim c As Object\nDim d As Object\nDim e As Boolean\n\na = ""\nb = 3\nSet c = Me.cmdCommand1\nSet d = CreateObject("Project1.Class1")\ne = False\n\nDebug.Print TypeName(a)\nDebug.Print TypeName(b)\nDebug.Print TypeName(c)\nDebug.Print TypeName(d)\nDebug.Print TypeName(e)\nEnd Sub\n</code></pre>\n\n<p>Results:</p>\n\n<pre><code>String\nInteger\nCommandButton\nClass1\nBoolean\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/52160', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1111/']