IsObject 函数IsObject function

返回一个指示标识符是否表示某个对象的变量Boolean 值。Returns a Boolean value indicating whether an identifier represents an object variable.



必需的_标识符_参数是变量名称。The required identifier argument is a variable name.


IsObject仅用于确定Variant是否为VarType vbObjectIsObject is useful only in determining whether a Variant is of VarType vbObject. 如果变量实际引用 (或引用) 对象, 或者如果它不包含**任何内容** , 则可能会出现这种情况。This could occur if the Variant actually references (or once referenced) an object, or if it contains Nothing.

如果 identifier 是一个使用 Object 类型或任何有效类型声明的变量,或者如果 identifierVarType ****vbObjectVariant 或是用户定义的对象,则 IsObject 会返回 True;否则会返回 FalseIsObject returns True if identifier is a variable declared with Object type or any valid class type, or if identifier is a Variant of VarType vbObject, or a user-defined object; otherwise, it returns False.

即使变量已被设置为 NothingIsObject 也会返回 TrueIsObject returns True even if the variable has been set to Nothing. 使用错误捕获以确保对象引用是有效的。Use error trapping to be sure that an object reference is valid.


此函数在代码的 "错误处理" 部分中非常有用, 在这种情况下, 您不能确定对象是在错误发生前实例化的, 例如, 您想要将其关闭。This function is useful in error handling sections of the code where you are not sure whether an object was instantiated before the error occurred, and for example, you want to close it.


此示例使用 IsObject 函数来确定标识符是否表示一个对象变量。This example uses the IsObject function to determine if an identifier represents an object variable. _MyObject_和_YourObject_是相同类型的对象变量。MyObject and YourObject are object variables of the same type. 它们是仅用于说明用途的通用名称。They are generic names used for illustration purposes only.

Dim MyInt As Integer              ' Declare variables.
Dim YourObject, MyCheck           ' Note: Default variable type is Variant.
Dim MyObject As Object
Set YourObject = MyObject         ' Assign an object reference.
MyCheck = IsObject(YourObject)    ' Returns True.
MyCheck = IsObject(MyInt)         ' Returns False.
MyCheck = IsObject(Nothing)       ' Returns True.
MyCheck = IsObject(Empty)         ' Returns False.
MyCheck = IsObject(Null)          ' Returns False.

另请参阅See also

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.