我在 VBA 中为 powerpoint 2010 选择了两个形状,如何利用它们创建某种数据结构
我正在尝试创建一个类似于此处的记忆游戏: http://www.mathsisfun .com/games/memory/index.html。我是这样处理的:
用户选择两个形状,然后启动一个名为 AssociateShapes() 的宏, 在 AssociateShapes 中,我使用名为 ShapeAssociation 的类模块,其中有两个属性 shape1Name 和 shape2Name,其中该对表示这些名称的形状之间的关联。
因此,当我尝试测试 ShapeAssociation 时,我使用此模块:
Global shapeAssociations() As ShapeAssociation
Global shapeAssoc As New ShapeAssociation
Public Sub Test()
ReDim shapeAssociations(0)
shapeAssoc.shape1Name = ActiveWindow.Selection.ShapeRange(1).Name
shapeAssoc.shape2Name = ActiveWindow.Selection.ShapeRange(2).Name
Set shapeAssociations(0) = shapeAssoc
MsgBox shapeAssociations(0).shape1Name
End Sub
但是,我最终看到的消息框是空的,可能表明 shapeAssoc 变量尚未初始化。我尝试了几种其他方法来在 VBA 中创建形状对的数据结构,从多维形状数组到此方法。所有这些尝试都会带来自己的错误消息,无论是编译时消息还是运行时消息。所以我的问题是,如何在 VBA 中创建表示一对形状的数据结构?
I am trying to create a memory game similar to the one here: http://www.mathsisfun.com/games/memory/index.html. I am approaching it like this:
The user selects two shapes and then starts a macro named associateShapes(),
In associateShapes I use a Class Module named ShapeAssociation, in it I have two properties shape1Name and shape2Name, where the pair represents an association between those the shapes of those names.
So when I try to test ShapeAssociation, I use this module:
Global shapeAssociations() As ShapeAssociation
Global shapeAssoc As New ShapeAssociation
Public Sub Test()
ReDim shapeAssociations(0)
shapeAssoc.shape1Name = ActiveWindow.Selection.ShapeRange(1).Name
shapeAssoc.shape2Name = ActiveWindow.Selection.ShapeRange(2).Name
Set shapeAssociations(0) = shapeAssoc
MsgBox shapeAssociations(0).shape1Name
End Sub
However, the Message Box I end up seeing is empty, possibly indicating that the shapeAssoc variable hasn't been initialized. I have tried several other approaches for creating a data structure of pairs of shapes in VBA, from a multiple dimension array of shapes to this approach. All these attempts bring their own error messages, be it compile time messages or run time messages. So my question to you is, how do I create a data structure in VBA that represents a pair of Shapes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于我们看不到类模块中发生的情况,因此我对其进行了一些重写以使用 UDT 数组。它的工作原理如下:
您需要验证用户实际上是否选择了至少两个形状 (Selection.ShapeRange.Count),并且仅选择了两个形状。
另外,您打算让用户在普通视图还是幻灯片视图中玩游戏?如果是后者,这些都不起作用;您无法在幻灯片视图中选择任何内容。
Since we can't see what's going on in your class module, I rewrote it a bit to use an array of UDTs instead. It works like so:
You'd want to verify that the user's actually selected at least two shapes (Selection.ShapeRange.Count), and only two shapes.
Also, do you plan to have the user play the game in Normal view or in SlideShow view? If the latter, none of this will work; you can't select anything in slideshow view.