【问题标题】:How to loop through immediately nested shapes in a group (VBA)如何遍历组中的立即嵌套形状(VBA)
【发布时间】:2021-12-02 17:10:23
【问题描述】:

我有一张用 Excel 制作的图表。手动将其复制到 PowerPoint。然后我将其取消分组两次。然后我选择了左侧的轴形状(数字,单独的轴)并组成了一个组。然后我采用图表的矩形表示数据,并将它们分组为 1)一组正值和 2)一组负值。所以我现在有 3 组形状。我以为我可以循环这三个组,但在 GroupItems 中,所有组都与其他形状混合在一起。我的想法是,我可以简单地检测 Sh.ParentGroup.Id 以找出有多少父母存在,并通过 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. 任何其他提示如何区分这三组形状?

【问题讨论】:

    标签: vba powerpoint shapes


    【解决方案1】:

    关键是要测试形状的类型。如果 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
    

    【讨论】:

      猜你喜欢
      • 2021-08-18
      • 2010-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-16
      • 2017-05-13
      • 2017-09-08
      • 1970-01-01
      相关资源
      最近更新 更多