2010-09-10 44 views

回答

8

对象变量,使用TypeOf ... Is

If TypeOf VarName Is TypeName Then 
    ''# ... 
End If 

例如:

Dim fso As New Scripting.FileSystemObject 

If TypeOf fso Is Scripting.FileSystemObject Then 
    Debug.Print "Yay!" 
End If 
5

只需添加到@托默勒格的答案...如果对象变量没有被实例化,然后用typeof测试会导致运行时错误。还要注意,该课程可能会实现接口

Dim fs As Scripting.FileSystemObject 

On Error Goto Err_Handler 

If TypeOf fs Is Scripting.FileSystemObject Then 
    Debug.Print "[Won't get here]" 
End If 

Err_Handler: 

If Err.Number <> 0 Then 
    Debug.Print "Oops, error when fs Is Nothing" 
End If 

On Error Resume Next 

Set fs = New Scripting.FileSystemObject 

If TypeOf fs Is Scripting.FileSystemObject Then 
    Debug.Print "Is a FileSystemObject" 
End If 

If TypeOf fs Is IFileSystem Then 
    Debug.Print "Implements IFileSystem " 
End If 
1

试试这个。

dim obj as object 
for each obj in me 
    debug.print TypeName(obj) 
next 
+0

谢谢,但做检查对象的类型,我们必须修改现有的代码有没有像它移动光标ORER或类似快速监视在那里我可以看到它的类型 – Avinash 2010-09-21 10:22:44

+0

感谢提供answeers任何方法.... :) – Avinash 2010-10-04 15:15:24

相关问题