2

Thanks Doug :-)

I need an idea for a non-programmer how to achieve iterating through groups.

I started with SO which works fine as long as I only take the .Names of the shapes.

But I need to try to check the type of every item in the group too I have a whole subset of questions to the shape (Sub CheckTextConformity)

This is the code as it runs - but ignores Groups. I started with the idea to call A subroutine for groups - but then what if the Group contains groups too etc.?

From Sub CheckAndReportOhneGroups() I call Sub WhatTypes... and depending on the type I call CheckTextConformity to give me information about the shape (especially text info).

ekad
  • 14,436
  • 26
  • 44
  • 46
mart stills
  • 25
  • 1
  • 4
  • 2
    Welcome to SO. Your question would be easier to read, and maybe answer, if you edit out the meandering self-criticism and focus on a clear description of what you need to do and where it's failing. It's great that you included code, but it's hard to tell which parts you need help with. We all begin somewhere and people on this site understand that :). – Doug Glancy Sep 15 '13 at 15:28

1 Answers1

8

To deal with groups (and possibly groups within groups) use something like this:

Sub Example()

Dim oSh As Shape
Dim oSl As Slide

For Each oSl In ActivePresentation.Slides
    For Each oSh In oSl.Shapes
        If oSh.Type = msoGroup Then
            'Debug.Print "GROUP"
            Call DealWithGroups(oSh)
        Else
            Debug.Print oSh.Name & vbTab & oSh.Type
        End If
    Next
Next

End Sub

Sub DealWithGroups(oSh As Shape)
    Dim x As Long
    Debug.Print "GROUP"
    For x = 1 To oSh.GroupItems.Count
        If oSh.GroupItems(x).Type = msoGroup Then
            Call DealWithGroups(oSh.GroupItems(x))
        Else
            Debug.Print vbTab & oSh.GroupItems(x).Name & vbTab & oSh.GroupItems(x).Type
        End If
    Next
End Sub

Yes. The snake is eating its own tail. ;-)

Steve Rindsberg
  • 14,442
  • 1
  • 29
  • 34
  • I was afraid to call my own Sub -- thanks for pointing me to this, Steve – mart stills Sep 16 '13 at 01:56
  • Great! This is the solution -- to take out the Group from the SELECT as worked out in [this article](http://stackoverflow.com/questions/16019942/find-the-type-of-a-shape) and instead check first for a group. Steve - you saved my day! I have no reputation here yet to vote for your answer, sorry. – mart stills Sep 16 '13 at 02:26
  • No worries ... thanks is thanks enough. Glad it helped. – Steve Rindsberg Mar 31 '14 at 03:17
  • @SteveRindsberg this doesn't seem to catch subgroups. instead it list all elements on the first level. Do you have any ideas how to determine if the shape is part of another group? – Cilvic Dec 01 '15 at 09:53
  • It does list each shape and each member of each group, even if groups include groups. I'm not sure why it's not working for you, unless perhaps you're running DealWithGroups rather than Example to start. – Steve Rindsberg Dec 02 '15 at 04:03
  • It seems there are no sub groups at all: I grouped 3 text boxes, and then grouped this group with 3 ungrouped text boxes (yeah this sentence isn't really clear), and I never get recursive when dealing with the group, I have a shape with 6 children, not 4 and a child with 3. By the way, if you check the group in the GUI, you don't see a group inside, you see 6 text boxes, so it's the same. Only when you ungroup you can see again the nested group. (okay I've said too much "group" for a day) – Rafiki Oct 29 '20 at 10:00
  • @Rafiki Can you post an example file somewhere so we can take a look? And by the way, there's another question in this thread that was deleted for other reasons, but it asked about what happens when the shape is a placeholder; placeholders cannot be included in groups, so will never be type msoGroup or be part of an msoGroup shape or contain groups. – Steve Rindsberg Oct 29 '20 at 15:54
  • @SteveRindsberg, surprisingly, your recursion is unnecessary because `oSh.GroupItems` can not contain subgroups, but rather will flatten out the group hierarchy completely and just contain the remaining shapes. In order to access subgroups, one must `Ungroup` the parent group first. See [here](https://stackoverflow.com/a/74339634/12287457) for a recursive example. To create an example file just create a new ppp with 3 shapes. Group two of them together, this will be the 'subgroup'. Then group the 'subgroup' with the remaining shape. Using your script, the subgroup's name won't be printed. – GWD Nov 07 '22 at 10:33
  • @GWD Thanks for pointing this out. I'm pretty sure that this is relatively new behavior. I'll have to try this out in a couple of older versions of PowerPoint. There's a potential problem with ungrouping/re-grouping, though. Any animations or action settings that have been applied to the group shapes will be lost; the group shape is deleted when you ungroup it. Upvoted your answer in the other thread, just the same. Nice work! – Steve Rindsberg Nov 09 '22 at 02:54
  • @SteveRindsberg I'm aware that un- and regrouping is not ideal and suspected it could cause some problems like the one you described. It's really annoying and unintuitive behavior. Unfortunately, I don't see another possibility except for maybe retrieving it from the XML stored in the file manually as proposed [here](https://stackoverflow.com/a/72538531/12287457). This would require the file to be saved and even then you'd need an implementation of the deflate algorithm in VBA to read the zip archive, which to my knowledge doesn't exist (yet). – GWD Nov 09 '22 at 22:51
  • 1
    @GWD Totally agree. Reading the XML in the zip is entirely possible, given a file saved to disk. I have an add-in that does this using an external component or it can be done with Windows-native options (https://exceloffthegrid.com/vba-cod-to-zip-unzip/ for example). Still, it'd take a very detailed understanding of how groups and subgroups are represented in the XML. – Steve Rindsberg Nov 10 '22 at 15:24