2009-06-18 113 views
1

在某些附加到Excel 2003电子表格的VBA中,我需要使用一些需要一段时间才能实例化的对象 - 所以我只想执行一次'设置'的事情...如何知道对象是否已被引用?

这更容易显示代码比写一个解释!

' 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 

如何检查myObj是否已设置?

我试过IsNull(myObj)和IsEmpty(myObj) - 无论myObj的状态如何都跳过'set'。我不能做

if myObj = Nil then 

if myObj = Empty then 

if myObj = Nothing then 

任何想法?

SAL

+0

您是否尝试过CreatObject? – THEn 2009-06-18 16:54:10

+0

这个页面有关于VB/VBA中空,空和空白之间差异的很好的信息http://beta.blogs.msdn.com/ericlippert/archive/2003/09/30/53120.aspx – Lunatik 2009-06-19 05:31:14

回答

6

这应该工作:

If myObj IS Nothing Then 

(注意是“IS”),如果还是不行,那么就必须通过类具体实现异步intialization因为COM初始化调用是同步的默认。因此,您需要检查文档,或与开发人员讨论Big类的某些属性或同步方法,以供您等待。

相关问题