Search code examples
vbapowerpoint

Assigning shapes to a group in Powerpoint using VBA


I have a set of slides with grouped shapes. The groups are named for identification. I need to make changes to the layout of the shapes in the groups and its easier if I ungroup them.

The challenge is
a) making sure when regrouping all the original shapes are included and
b) the original group name is lost when regrouped.

The first macro ungroups the shapes and stores the group name in an offslide textbox and all the shape names in a second offslide text box. The code below is to recreate that grouping once the changes have been made

If I have a list of shape names, how can I add them to a group using VBA?

The list of shape names is dynamic, i.e. the names of the shapes and number of shapes can change each time I run the code.

For example, I have a list of shape names "shape1", "shape2", "shape3", "shape4". They are in a string array. I loop through the array adding each shape into a shape array but I can't get the .Group method to work. I tried passing the list of shaped to group as strings, shape arrays.

'Using a static example to keep it simple but this list of shapes can change
strContents = "shape1,shape2,shape3,shape4)"
strGroupMembers = Split(strContents, ",")
ReDim shpArray(0 To UBound(strGroupMembers) - 1)

For intCounter = 0 To UBound(strGroupMembers) - 1
    Set shpArray(intCounter) = ActiveWindow.View.Slide.Shapes.Range(strGroupMembers(intCounter))
Next
'This line is causing the error
ActiveWindow.View.Slide.Shapes.Range(shpArray).Group

I tried to pass the list of shapes to the group method as a comma delimited string in a variable, and now trying to create an array of shapes and passing that argument but always get a object required error.


Solution

  • The Range method takes an array of strings. Since you didn't declare it, it became an array of variants instead.

    Dim strGroupMembers() As String
    strContents = "shape1,shape2,shape3,shape4"
    strGroupMembers = Split(strContents, ",")
    
    ActiveWindow.View.Slide.Shapes.Range(strGroupMembers).Group
    

    Another way:

    shpArray = Array("shape1", "shape2", "shape3", "shape4")
    ActiveWindow.View.Slide.Shapes.Range(shpArray).Group