vbapowerpointshapesactiveview

Powerpoint VBA Make duplicated shape view active to select for grouping


I have a library of eight images on my PowerPoint slide. Based on a userform input, some of the components get duplicated and renamed by adding a "1" or "2" after the original image's name so that they are differentiable. I then want to group the new images (I am building up an item from the component images). I am able to duplicate the images and line them up correctly, but I am having trouble grouping them. Note that I am not always grouping the same number of items, it is dependent on user inputs.

I receive the error "Shape (unknown member): Invalid request. To select a shape, its view must be active."

I searched and attempted to implement several strategies from the help forums but am coming up empty.

PLEASE HELP!!! -Kevin

Part of code below because it is very long, but this is where my first problem arises:

Dim Cargo As Shape, Cargo_Dup as Shape, Chemical as Shape, Chemical_Dup as Shape
Set Cargo = ActivePresentation.Slides(2).Shapes("Cargo")
Set Chemical = ActivePresentation.Slides(2).Shapes("Chemical")
Cargo.Name = "Cargo"
Chemical.Name = "Chemical"

With ActivePresentation
Set Cargo_Dup = ActivePresentation.Slides(2).Shapes("Cargo")
    With Cargo_Dup.Duplicate
        .Name = "Cargo_1st"
        .Left = 0
        .Top = 540
    End With
'CHEMICAL
If Input1 = "Chemical" Then
    Set Chemical_Dup = ActivePresentation.Slides(2).Shapes("Chemical")
        With Chemical_Dup.Duplicate
            .Name = "Chemical" & 1
            .Left = 36.74352
            .Top = 540 + 0.36
        End With
   '''''WHERE PROBLEM ARISES'''''
    ActivePresentation.Slides(2).Shapes("Cargo_1st").Select
    ActivePresentation.Slides(2).Shapes("Chemical1").Select msoFalse
    Set Vehicle = ActiveWindow.Selection.ShapeRange.Group
    Vehicle.Name = "Vehicle"
'Elseif with a bunch for options where addition grouping occurs

Solution

  • I need some kind of keyboard macro to type this for me:

    Never select anything unless you absolutely have to. You nearly never absolutely have to.

    You're asking how to make a view active so that you can select something. I figure that's the wrong question.
    It's more useful to know how to work with shapes WITHOUT having to select them. Grouping shapes w/o selecting them is a bit tricky, but it can be done.

    Here's an example of how you might go about this:

    Sub GroupWithoutSelecting()
    
        Dim oSl As Slide
        Dim oSh As Shape
        Dim aShapes() As String
    
        Set oSl = ActivePresentation.Slides(2) ' or whichever slide you like
        ReDim aShapes(1 To 1)
    
        With oSl
            For Each oSh In .Shapes
                If oSh.Type <> msoPlaceholder Then ' can't group placeholders
    
                    ' Substitute the real condition you want to use
                    ' for selecting shapes to be grouped here
                    If oSh.Type = msoAutoShape Then
                        ' add it to the array
                        aShapes(UBound(aShapes)) = oSh.Name
                        ReDim Preserve aShapes(1 To UBound(aShapes) + 1)
                    End If
    
                End If
            Next
    
            ' eliminate the last (empty) element in the array
            ReDim Preserve aShapes(1 To UBound(aShapes) - 1)
    
            ' Create a shaperange from the array members and group the shaperange
            .Shapes.Range(aShapes).Group
    
        End With ' oSl
    
    End Sub