Search code examples
vbapowerpointinterop

How can I convert a graphic into a form by program as I can do it in PowerPoint UI from context menu?


In the PowerPoint UI I can convert a graphic into AutoShapes using the Group>Ungroup command in the context menu. It is converted into a group of shapes. Additionally it is possible to regroup them. Why are these methods either missing (in case of conversion) or raising errors (in case of ungrouping) for msoShapeType = 28?


Solution

  • Here's an example that works here. Note that some of the shapes in ungrouped SVGs can be edited using Edit Points but the EMF version cannot. I don't know why that'd be. A limitation of EMF, perhaps.

    Sub TestConvert()
    ' Ungroups the currently selected icon shape
    
    Dim oSh As Shape
    Dim oSl As Slide
    
    Set oSh = ActiveWindow.Selection.ShapeRange(1)
    Set oSl = oSh.Parent
    
    ' Copy the shape then paste it back as an EMF
    oSh.Copy
    ActiveWindow.View.PasteSpecial ppPasteEnhancedMetafile
    ' At this point you might want to oSh.Delete to remove the original shape
    
    ' get a reference to the newly pasted EMF
    Set oSh = oSl.Shapes(oSl.Shapes.Count)
    ' Ungroup it
    Set oSh = oSh.Ungroup(1)
    ' Ungroup it again
    oSh.Ungroup
    
    End Sub