如何循环遍历组中的立即嵌套形状 (VBA)

How to loop through immediately nested shapes in a group (VBA)

我有一张我在 Excel 制作的图表。手动将其复制到 PowerPoint。然后我取消了两次分组。然后我选择了左边的轴形状(数字,单独的轴)并做了一个组。然后我用图表的矩形表示数据,并将它们分组为 1) 一组用于正值,2) 一组用于负值。所以我现在有 3 组形状。我以为我可以循环这三个组,但在 GroupItems 中,所有组都与其他形状混合在一起。我想到了这个想法,我可以简单地检测 Sh.ParentGroup.Id 来找出有多少 parents 存在并通过 ParentGroup.Id 将它们分成三组......所以我做的第一件事是一个试图访问 Sh.ParentGroup.Id 的简单循环。但是,如果我添加一个手表,它会在第 4 次迭代时崩溃,而我 运行 代码的 Visual Basic 和 PowerPoint 会重新启动。

For Each Sh In ActiveWindow.Selection.ShapeRange.GroupItems
' GroupParentsIDs(Sh.ParentGroup.Id) = Sh.ParentGroup.Id
MsgBox ""
Next Sh
  1. 你知道它崩溃的原因吗?我应该检测 ParentGroup 成员是否存在吗?如果需要查,怎么查?
  2. 还有其他技巧可以区分这三组形状吗?

关键是要测试形状的类型。如果它不是一个组,ParentGroup 将失败。像这样:

Dim sh As Shape
Dim SubSh As Shape
For Each sh In ActivePresentation.Slides(1).Shapes
    Debug.Print sh.Id
    If sh.Type = msoGroup Then
        For Each SubSh In sh.GroupItems
            Debug.Print "  " & SubSh.Id
        Next SubSh
    End If
Next sh