我如何知道某个对象是否已被引用?

SAL*_*SAL 1 excel vba excel-2003 excel-vba nothing

在附加到Excel 2003电子表格的某些VBA中,我需要使用一些需要一段时间才能实例化的对象 - 所以我只想做一次'set'事情......

显示代码比编写解释更容易!

' Declare the expensive object as global to this sheet
Dim myObj As SomeBigExpensiveObject

Private Sub CommandButtonDoIt_Click()

   ' Make sure we've got a ref to the object
   If IsEmpty(myObj) Then  ' this doesn't work!
      Set myObj = New SomeBigExpensiveObject
   End If

   ' ... etc

End Sub
Run Code Online (Sandbox Code Playgroud)

如何检查myObj是否已设置?

我尝试过IsNull(myObj)和IsEmpty(myObj) - 无论myObj的状态如何,都跳过'set'.我做不到

if myObj = Nil then
Run Code Online (Sandbox Code Playgroud)

要么

if myObj = Empty then
Run Code Online (Sandbox Code Playgroud)

要么

if myObj = Nothing then
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

SAL

RBa*_*ung 6

这应该工作:

    If myObj IS Nothing Then
Run Code Online (Sandbox Code Playgroud)

(注意"IS")如果这不起作用,那么必须由该类专门实现异步初始化,因为默认情况下COM init调用是同步的.因此,您需要检查文档,或者与开发人员讨论Big类的某些属性或者同步方法,以便您等待.