Im getting a error: "Runtime error 1004 Unable to get the group Property of the DrawingObJects class" My questions in problem statement

Viewed 32

When I run this subroutine, there is always 2 ungrouped shapes present on the sheet "Symbol Editor" so CASE 2 is always executed.

When the statement 'Selection.Name = "testThis"' Executes fine. "Set grpShapes = Selection.Group" however, when the statement: "Set grpShapes = Selection.Group" I get the following error

"Runtime error 1004 Unable to get the group Property of the DrawingObJects class" My Questions are:

  1. Why can I assign a value to a selection.group "property", but cannot capture the object itself?
  2. This question is "semi" related. Each shape has its own positioning coordinates, but once "grouped" the entire group can be "selected" and moved around a SHEET in unison. How is this done? Does the selection have its own positioning coordinate and each shape adjusts their coordinates accordingly?

'''''

  Sub TestForGroups()
  Dim shps As Shapes: Set shps = Sheets("Symbol Editor").Shapes
  Dim grpShapes As GroupShapes 
  Dim TN As String

Select Case shps.Count
    *Case 0
        TN = TypeName(Selection)
        MsgBox "There are no shapes ('" & TN & "').", vbInformation
     Case 1
         shps.SelectAll
         TN = TypeName(Selection)
         If TN = "GroupObject" Then
            Selection.Ungroup
            MsgBox "Ungrouped a group ('" & TN & "').", vbInformation
         Else
            MsgBox "One shape selected ('" & TN & "').", vbInformation
         End If
         Debug.Print TypeName(Selection)
     Case Is > 1
         shps.SelectAll
         TN = TypeName(Selection)
         If TypeName(Selection) = "DrawingObjects" Then
            Selection.Group            
            MsgBox "Grouped all shapes ('" & TN & "').", vbInformation
            Selection.Name = "testThis"
            **Set grpShapes = Selection.Group** 
         End If
End Select    
    

End Sub ''''

1 Answers

To access the GroupShapes object, you go through the GroupItems like this:

Sub CreateMyGroup()
    Dim thisSheet As Worksheet
    Set thisSheet = Sheets("Symbol Editor")
    
    Dim allShapes As Shapes
    Set allShapes = thisSheet.Shapes
    
    allShapes.SelectAll
    If TypeName(Selection) = "DrawingObjects" Then
        Dim theGroup As Object
        Set theGroup = Selection.Group
        theGroup.Name = "MyBigGroup"
        
        Dim grpShapes As groupShapes
        Set grpShapes = theGroup.ShapeRange.GroupItems
        Debug.Print "shapes in this group = " & grpShapes.Count
    End If
End Sub
Related