How to create and insert shapes in PowerPoint with VBA?

Couple days ago, i wanted to automate the rather mundane task of creating some PowerPoint slides. When automating PowerPoint you typically work with the Slides and Shapes collection. Slides is rather self explanatory, and shapes is pretty much everything else including text boxes, action buttons, media objects, labels, pictures, callouts, flowcharts and so on and so forth. Although i will be showcasing the techniques with PowerPoint, some of the syntax is actually pretty applicable to Microsoft Word and Excel.

Just to recap, before you get started with VBA macro development in PowerPoint you need to have your development tab enabled.

First off, let’s start by automatically creating a presentation with some slides as shown in this tutorial: creating a presentation in VBA.

Now we can start to work with Shapes. First off, let’s go ahead and open the VBA Editor by hitting Alt+ F11.If you followed the previous tutorial, you should have module 1 in your Modules list (as shown below)

Working with Shapes with VBA

Adding a text box with VBA

We’ll start by adding a vertical text box to our first slide in the presentation. Add the following code to Module1, then go ahead and hit the Save button (Ctrl+s) and Run your macro (hit F5 or hit Run Sub/User Form).

Sub CreateTextBox()
Set MySlide = ActivePresentation.Slides(2)

    With MySlide.Shapes
        .AddTextbox(Orientation:=msoTextOrientationVertical, _
         Left:=90, Top:=200, Width:=80, _
         Height:=200).TextFrame.TextRange.Text _
        = ("This is my vertical text box")

End With

End Sub

Note that you are able to resize your text box dimensions rather quite easily with VBA. In this case we’ll resize the first shape in the second slide, feel free to modify as needed.

Sub ResizeText()
Set MyShape = ActivePresentation.Slides(2).Shapes(1)

'Add your required dimensions as needed below
   With MyShape
         .Width = 200
         .Height = 35

End With
End Sub

Text Effects with VBA

Let us now assume that we want to add a text box to all slides in the presentation, this time a bit more visually appealing. To do that we’ll use VBA to create custom Text effects which we are able to embed into one or more slides in the presentation. We’ll first going to loop through the presentation slides and then add the text effect as needed.

Sub SetEffects()

Dim i As Integer

For i = 1 To ActivePresentation.Slides.Count
    ActivePresentation.Slides(i) _
    .Shapes.AddTextEffect msoTextEffect12, "Draft for Review", _
    "Segoe UI", 32, msoTrue, msoTrue, 650, 50

End Sub

Here’s the result ;-):

Note that you might as well use a simple watermark to accomplish this specific result.

PowerPoint callouts with VBA

Our next example will be adding callouts to your presentation. In this exmaple we’ll add a callout the the second slide.

Sub CreateCallout()

ActivePresentation.Slides(2).Shapes.AddCallout(Type:=msoCalloutTwo, Left:=200, Top:=50, _  Width:=300, Height:=100).TextFrame.TextRange.Text = "My Callout"

End Sub