Search code examples
vbapowerpointshapespresentationpowerpoint-2010

VBA PowerPoint 2016 shapes BuildFreeform properties after .ConvertToShape


After creating a shape with .ConvertToShape, what is its index in Shapes? And how do I give it a Line colour? I want to create several msoFreeform shapes, and give them different colours. I have come this far:

With myDocument.Shapes.BuildFreeform(EditingType:=msoEditingCorner, X1:=X(1), Y1:=Y(1))
    For i = 1 To 361
    .AddNodes SegmentType:=msoSegmentLine, EditingType:=msoEditingAuto, X1:=X(i), Y1:=Y(i)
    Next i
    .ConvertToShape
End With

For Each shp In ActivePresentation.Slides(1).Shapes
    If shp.Type = 5 Then 'msoFreeform
    shp.Line.ForeColor.RGB = RGB(0, 0, 64) 'this will however colour all in the same colour
    shp.Line.Weight = 2.5
    End If
    Debug.Print shp.Type
Next shp

I would like to give a colour to the freeform created, then create another freeform and give it another colour, and so on, for several freeforms. Thanks for any help.


Solution

  • As a general rule, it's a good idea to provide an example that runs on its own, so that anyone who'd like to help can start with a simple copy/paste rather than having to adapt your code.

    In any case, .ConvertToShape returns a reference to the newly created shape, so you can immediately use that reference to set color or whatever properties you like. Here I'm just grabbing the new shape's name and displaying it in a messagebox:

    Sub TryThis()
    
    Dim oSh As Shape
    Dim i As Long
    
    With ActivePresentation.Slides(1).Shapes.BuildFreeform(EditingType:=msoEditingCorner, X1:=x(1), Y1:=y(1))
        For i = 1 To 361
        .AddNodes SegmentType:=msoSegmentLine, EditingType:=msoEditingAuto, X1:=x(i), Y1:=y(i)
        Next i
        Set oSh = .ConvertToShape
        MsgBox oSh.Name
        
    End With
    
    End Sub