Search Type Runtime VB6

How can you get the Type (name as string is enough) of an object in VB6 at runtime?

i.e. something like:

If Typeof(foobar) = "CommandButton" Then ... 

/ EDIT: to clarify, I need to check objects with dynamic type. Example:

 Dim y As Object Set y = CreateObject("SomeType") Debug.Print( <The type name of> y) 

If the output is "CommandButton"

+6
vb6 runtime
source share
4 answers

I think you are looking for TypeName, not TypeOf.

 If TypeName(foobar) = "CommandButton" Then DoSomething End If 

Edit: what do you mean by dynamic objects? You mean objects created using CreateObject ("") because this should work.

Edit:

 Private Sub Command1_Click() Dim oObject As Object Set oObject = CreateObject("Scripting.FileSystemObject") Debug.Print "Object Type: " & TypeName(oObject) End Sub 

Outputs

Object Type: FileSystemObject

+8
source share

I don't have a copy of VB6, but I think you need

 Typename() 

function ... I see this in Excel VBA, so it is probably at the same runtime. Interestingly, the help seems to suggest that it should not work for a custom type, but this is the only way I've ever used it.

Help file excerpt:

TypeName Function

Returns a string that provides information about the variable.

Syntax

TypeName (VarName)

The required argument to varname is a Variant containing any variable except a user-type variable.

+2
source share

TypeName is what you want ... Here is an example output:

VB6 Code:

 Private Sub cmdCommand1_Click() Dim a As Variant Dim b As Variant Dim c As Object Dim d As Object Dim e As Boolean a = "" b = 3 Set c = Me.cmdCommand1 Set d = CreateObject("Project1.Class1") e = False Debug.Print TypeName(a) Debug.Print TypeName(b) Debug.Print TypeName(c) Debug.Print TypeName(d) Debug.Print TypeName(e) End Sub 

Results:

 String Integer CommandButton Class1 Boolean 
+2
source share

This should be tricky since in VB6 all objects are COM ( IDispatch ). Thus, they are only an interface.

TypeOf(object) is class perhaps only a COM call get_interface (I forgot the name of the exact method, sorry).

0
source share

All Articles