2011-02-23 63 views
12

我有一个类模块,命名Normal,在VBA用下面的代码VBA类的设置属性:与对象引用

Private mLine As LineElement 

Public Property Get Line() As LineElement 
    Line = mLine 
End Property 

Public Property Set Line(ByRef vLine As LineElement) 
    mLine = vLine 
End Property 

此类用于通过下面的代码:

Sub Run 
    Dim Line As LineElement 
    Set Line = New LineElement 

    Dim Norm As Normal 
    Set Norm = New Normal 
    Set Norm.Line = Line 'FAILS here with "Object Variable or With Block Variable not set"' 
End Sub 

另外,如果我将Normal班级模块中的代码更改为:

Private mLine As LineElement 

Public Property Get Line() As LineElement 
    Line = mLine 
End Property 

Public Sub SetLine(ByRef vLine As LineElement) 'changed from property to sub' 
    mLine = vLine 
End Property 

and failin g行到

Norm.SetLine(Line) 

我得到一个“对象不支持此属性或方法”错误。我在这两种情况下究竟做错了什么?

回答

18

试试这个:

+4

D'那么简单哦!谢谢您的帮助。 (我恨VBA编辑器,它不提供更多的帮助,像这样简单的事情) – 2011-02-23 15:12:37

+0

双德!花费的时间追逐不能分配给ActiveX。谢谢。 – 2015-10-15 21:52:30

3

这两个属性必须有“设置”关键字

Private mLine As LineElement 

Public Property Get Line() As LineElement 
    Set Line = mLine 'Set keyword must be present 
End Property 

Public Property Set Line(vLine As LineElement) ' ByRef is the default in VBA 
    Set mLine = vLine 'Set keyword must be present 
End Property 
0

SET语句来创建对象的一个​​对象变量的引用。如果您正在处理原始和本机内置类型(如整数,双精度,字符串等),则不必使用Set关键字。这里你正在处理一个LineElement类型的对象。

0

试试这个

Private mLine As new LineElement 

Public Property Get Line() As LineElement 
    Set Line = mLine 
End Property 

Public Property Set Line(ByRef vLine As LineElement) 
    Set mLine = vLine 
End Property