Consulting

Results 1 to 10 of 10

Thread: Determine selected item in GroupItems

  1. #1
    VBAX Contributor
    Joined
    May 2008
    Posts
    198
    Location

    Determine selected item in GroupItems

    I need to run an action on the current selection, but if the selection is one or more items in a group (note - NOT the group itself), I need to run the action on those items individually. The Shape.Type property comes up as a group whether it is the group itself selected, or a single item in the group. I looked to see if I could loop through the group items to determine which of them were selected, but haven't seen a way to do so.

    Is there any way to determine which items in a group are selected and which are not?

  2. #2
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,094
    Location
    You always have the good questions Mark!

    AFAIK there's no sensible way to check this.

    Here's a possible not sensible way:

    Sub Silly_chex()
    Dim myTop() As Single
    Dim grpTop As Single
    Dim i As Integer
    Dim strResult As String
    Dim oshp As Shape
    ReDim myTop(1 To 1)
    Set oshp = ActiveWindow.Selection.ShapeRange(1)
    grpTop = oshp.Top
    If oshp.Type <> msoGroup Then Exit Sub
    For i = 1 To oshp.GroupItems.Count
        myTop(i) = oshp.GroupItems(i).Top
        ReDim Preserve myTop(1 To UBound(myTop) + 1)
    Next
    ReDim Preserve myTop(1 To UBound(myTop) - 1)
    SendKeys "{DOWN}"
    DoEvents
    If grpTop = oshp.Top Then
        For i = 1 To oshp.GroupItems.Count
            If oshp.GroupItems(i).Top <> myTop(i) Then
                strResult = strResult & oshp.GroupItems(i).Name & vbCrLf
            End If
        Next
        MsgBox "These group shapes were selected:" & vbCrLf & strResult
    Else
        MsgBox " A group is selected but no group items were selected"
    End If
    SendKeys "{UP}"
    End Sub
    Last edited by Aussiebear; 04-19-2023 at 12:55 AM. Reason: Adjusted the code tags
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  3. #3
    MS Excel MVP VBAX Mentor Andy Pope's Avatar
    Joined
    May 2004
    Location
    Essex, England
    Posts
    344
    Location
    Assuming I have understood the "selection is one or more items in a group" part can you not use ChildShapeRange object?

    for each xx in activewindow.Selection.ChildShapeRange:xx.Fill.ForeColor.RGB=255:next xx
    Cheers
    Andy

  4. #4
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,094
    Location
    You learn something new every time Andy posts!
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  5. #5
    VBAX Contributor
    Joined
    May 2008
    Posts
    198
    Location
    That looks like it works, Andy! I had seen the 'childShapeRange' in the object model, but hadn't put it together that it would include the shapes selected from the group. I probably should have realized that, but it totally eluded me.

    Thanks to both of you; and of course I always have the good questions, John. I feel like I've already been hit by every other 'idiosyncracy' and 'quirk' that Microsoft has 'provided' in PowerPoint's VBA object model.

    I appreciate the assistance!

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Is there any way to determine which items in a group are selected and which are not?
    Maybe I took Cosmo too literally about selecting an item in a group.

    Follow on question ...

    How can you determine the selected .ChildShapeRange?

    If I have a group of 4 shapes and the second child is selected, how can I determine that?

    I got this far before getting stuck

    Option Explicit
    Sub SelectedShape()
    Dim i As Long
    With ActiveWindow.Selection
        If .HasChildShapeRange Then
            For i = 1 To .ChildShapeRange.ConnectionSiteCount
                MsgBox .ChildShapeRange.Child(i).Type ' does't work
            Next I
        End If
    End With
    End Sub

    Paul
    Attached Images Attached Images
    Last edited by Aussiebear; 04-19-2023 at 12:56 AM. Reason: Adjusted the code tags

  7. #7
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,094
    Location
    Assuming the shapes have text

    Sub which_ones()
    Dim i As Integer
    Dim strSel As String
    With ActiveWindow.Selection
        For i = 1 To .ChildShapeRange.Count
            strSel = strSel & .ChildShapeRange(i).TextFrame.TextRange & vbCrLf
        Next i
    End With
    MsgBox "Selected :" & vbCrLf & strSel
    End Sub
    Last edited by Aussiebear; 04-19-2023 at 12:56 AM. Reason: Adjusted the code tags
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  8. #8
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Got it

    I kept coming up with .ChildShapeRange.Count = 1 because I only had one 'sub shape' selected, and I was thinking that the .ChildShapeRange was referring to a collection of all the shapes within the Group.

    I missed the implication that .ChildShapeRange would be all of the sub-shapes selected.

    Thanks

    Paul

  9. #9
    VBAX Contributor
    Joined
    May 2008
    Posts
    198
    Location
    Quote Originally Posted by Paul_Hossler
    Maybe I took Cosmo too literally about selecting an item in a group.

    Follow on question ...

    How can you determine the selected .ChildShapeRange?

    If I have a group of 4 shapes and the second child is selected, how can I determine that?

    I got this far before getting stuck

    Option Explicit
    For i = 1 To .ChildShapeRange.ConnectionSiteCount

    Paul
    As John pointed out, you're using 'ConnectionSiteCount' (which is the # of places where a connector will 'snap' to: a square has 4; a circle has 8), where you need to use 'Count'

    Quote Originally Posted by Paul_Hossler
    I missed the implication that .ChildShapeRange would be all of the sub-shapes selected.
    That was where I was going wrong as well.
    Last edited by Aussiebear; 04-19-2023 at 12:57 AM. Reason: Adjusted the code tags

  10. #10
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Can I blame the on-help??

    Didn't think so

    That .ConnectionSiteCount was about the 5th or 6th thing I tried, after I completely missed the fact that the .Count approach was telling the the correct answer, but I didn't believe it

    Thanks to all

    Paul

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •