如何在VBA中将形状分配给Class对象?

问题描述 投票:0回答:2

我正在尝试创建一个可以处理形状(和其他事物)的类。

下面是我的班级中处理形状的部分。我班级中的每个对象都有一个与之关联的形状。我想存储并在需要时调用它。

类(CShapes):

Option Explicit
Private m_Shape As Shape

Public Property Get Shape() As Shape
    Set Shape = m_Shape
End Property

Public Property Let Shape(ByVal pShape As Shape)
    Set m_Shape = pShape
End Property

我的功能/方法:

Sub CustomShapes()
    Dim star As New CShapes ' Declare and instantiate the CShapes object
    Dim shp1 As Shape
    Set shp1 = ActiveSheet.Shapes.AddShape(msoShape16pointStar, _
      ActiveCell.Left, ActiveCell.Top, 80, 27)
    Set star.Shape = shp1 ' Set the Shape property of the CShapes object
End Sub

我收到错误

对象变量或未设置块

排队

Set star.Shape = shp1

我怀疑我定义类的方式是错误的。

excel vba
2个回答
2
投票

尝试使用 Property Set,而不是 Property Let...

Public Property Set Shape(ByVal pShape As Shape)

希望这有帮助!


0
投票

改变

Set star.Shape = shp1 

进入

star.Shape = shp1

否则你会要求 CShape 返回一个应设置为 shp1 的对象(不能以这种方式完成)。如果您注意到,随着

Set star.Shape = shp1
Get
被激活。通过
star.Shape = shp1
Let
被激活。

© www.soinside.com 2019 - 2024. All rights reserved.