我想获得某个组中所有形状的集合。组中的形状本身可能是一个组,所以我(我想)需要一个递归函数来获得每个形状。
这是我的代码:
…
你的问题从这一行开始:
shapes.Add (subshp) 'Add the subshp to the shape collection
应该是哪个(注意删除括号):
shapes.Add subshp 'Add the subshp to the shape collection
这里发生的事情是 () 意味着VBA评估 subshp 并返回默认方法返回的任何内容(例如名称或ID)。所以你没有添加 Shape 你的收藏品,但其他东西(例如 String 要么 Long )。
()
subshp
Shape
String
Long
这条线上的空间让我困惑:
Set col = getAllShapes (subshp) 'Get all the shapes from the group
你的VBE应该写成:
Set col = getAllShapes(subshp) 'Get all the shapes from the group
所以这告诉我其他事情正在发生。但是,在代码中的这一点 subshp 是一个 Shape 对象所以不应该造成任何问题。
所以现在,当您使用以下代码时,VBA正在寻找一个 Shape 但是你的收藏包含其他东西。这导致代码行不匹配 - 因此您的错误。
For Each colShp In col 'Error here shapes.Add (colShp) 'Add all the shapes to the collection Next colShp
你经常这样做吗?
shapes.Add (colShp) 'Add all the shapes to the collection
Else: shapes.Add (shp)
另外:不需要将两行连接到“:” - 这有助于混淆代码。 他们应该是:
shapes.Add colShp 'Add all the shapes to the collection
Else
shapes.Add shp
End If
最后一点,一个好主意是不要将变量(例如“形状”)命名为与现有常用方法或属性相同 - 这可能会导致您使用哪个参数时出现一些混淆。