You can use Visual Basic for Applications (VBA) to automate repetitive tasks and add dynamic content to your presentations. One good example is to create and customize shapes. In this tutorial, i will show how to add a shape, change its properties like color and size, and manipulate it further using PowerPoint VBA.
Open your PowerPoint VBA Editor
Hit your Developer tab and then Hit the Visual Basic Button. Alternatively hit the keystroke combination ALT + F11.
In the Project Explorer tree at the left hand side, right click any of your PowerPoint projects. Then select Insert and then hit Module. This will create a space for your VB code.
Insert your VBA Macro code
Now that your environment is set, you can add you VBA code to your file.
Create a new shape
I will first add a rectangular shape into our Active Slide – we will call our shape”Simple Rectangle”
Sub CreateRectangle()
' Add a rectangle to the active slide
With ActivePresentation.Slides(1).Shapes.AddShape(Type:=msoShapeRectangle, _
Left:=100, Top:=100, Width:=300, Height:=150)
.Name = "SimpleRectangle"
End With
End Sub
Add text to your shape
We can set the text caption, font size, color using VBA.
Sub EditShapeText()
' Add text to the shape and change font properties
With ActivePresentation.Slides(1).Shapes("SimpleRectangle").TextFrame.TextRange
.Text = "This is my rectangle"
.Font.Name = "Arial"
.Font.Size = 20
.Font.Bold = msoTrue
.Font.Color = RGB(0, 0, 0)
End With
End Sub
Change your rectangle color
We can obviously change the shape color using the ForeColor property:
Sub ChangeShapeColor()
With ActivePresentation.Slides(1).Shapes("SimpleRectangle").Fill
.ForeColor.RGB = RGB(255, 0, 0)
End With
End Sub
We can change the line weight, dash style and toggle its visibility using additional PowerPoint properties.
Here’s our code after being inserted in the VBA Module:
Run your code
Save your Module and hit ALT + F8. Then make sure to run your macros at the order posted in this tutorial. Here is our beautiful result 🙂