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

Time:10-15

I have a chart which I have made in Excel. Copied it manually to PowerPoint. Then I have ungrouped it twice. Then I have selected the axis shapes on the left (numbers, axis alone) and made a group. Then i have taken the rectangles of the Chart representing data, and grouped them as 1) one group for positive values and 2) one group for negative values. So I have 3 group of shapes now. I thought I could loop these three groups, but in GroupItems there are all the groups mixed together with other shapes. I came with the idea, that I could simply detect Sh.ParentGroup.Id to find out how many parents is present and to separate them on three groups by the ParentGroup.Id... So first thing I did is a simple loop trying to access Sh.ParentGroup.Id. However if I add a watch, it crashes on 4th iteration and the Visual Basic and PowerPoint, where I run the code, restarted.

For Each Sh In ActiveWindow.Selection.ShapeRange.GroupItems
' GroupParentsIDs(Sh.ParentGroup.Id) = Sh.ParentGroup.Id
MsgBox ""
Next Sh
  1. Do you have idea why it crashes? Should I detect if the ParentGroup member exists? If it is needed to check it, then how?
  2. Any other tips how to make it possible to differentiate between the three groups of shapes?

CodePudding user response:

The key is to test the type of shape. ParentGroup will fail if it isn't a group. Something like this:

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
  • Related