Powerpoint VBA Grouping is disabled for the selected shapes

145 views Asked by At

I am trying to create a Sub process which create a bunch of image and group it multiple times. For First loop everything runs OK VBA Code runs as expected, On Second run VBA gives the error "Grouping is disable for selected shapes"

Sub PPT_AddShape14
x = 8 * (i - 1) + 170 * (i - 1)
y = 0
Set sh1 = vslide.Shapes.AddShape(msoShapeRectangle, x + 8, y + 8, 170, 170)
Set sh2 = vslide.Shapes.AddShape(msoShapeRectangle, x + 8, y + 178, 170, 30)
Set sh3 = vslide.Shapes.AddShape(msoShapeRectangle, x + 8, y + 208, 170, 190)
vslide.Shapes.Range(Array("sh1", "sh2", "sh3")).Group.Name = Str(x)
Set Tiles = vslide.Shapes(Str(x))

End Sub
1

There are 1 answers

1
Steve Rindsberg On BEST ANSWER

It's always a good idea to put Option Explicit at the top of every module and to properly declare your variables. But the issue with your code is that Range takes the NAMES of shapes as arguments, not strings that happen to match the names you've given the object variables you're using. This works:

Option Explicit

Sub PPT_AddShape14()
Dim i As Long
Dim x As Long
Dim y As Long
Dim sh1 As Shape
Dim sh2 As Shape
Dim sh3 As Shape
Dim vslide As Slide
Set vslide = ActivePresentation.Slides(1) ' as test
Dim Tiles As Shape

x = 8 * (i - 1) + 170 * (i - 1)
y = 0
Set sh1 = vslide.Shapes.AddShape(msoShapeRectangle, x + 8, y + 8, 170, 170)
Set sh2 = vslide.Shapes.AddShape(msoShapeRectangle, x + 8, y + 178, 170, 30)
Set sh3 = vslide.Shapes.AddShape(msoShapeRectangle, x + 8, y + 208, 170, 190)
vslide.Shapes.Range(Array(sh1.Name, sh2.Name, sh3.Name)).Group.Name = Str(x)
Set Tiles = vslide.Shapes(Str(x))

End Sub