Add text to shape on slide using PowerPoint VBA

add text to shape in slide powerpoint vba

Objective

To change the text in shapes on the PowerPoint slide using VBA excel.

Approach

In this PowerPoint automation, we are changing the text inside rectangular shapes on slide 1. For demonstration purpose, we had 3 rectangles placed on slide 1. All 3 rectangles had text which started with the string “Revenue“ before executing the VBA code.

In the code below we loop through each rectangular shape on the slide, if the shape contains a text which starts with the word “Revenue”, then we shall change the text inside the shape. We can see that we have replaced the string “Revenue 2017” with “Sales 2017” for first box


Code

Sub AddTextInShapes()

Dim MyShape As Shape
Dim ShapeIndex As Integer

ShapeIndex = 2017

'Loop through each shape in first slide in ppt
For Each MyShape In ActivePresentation.Slides(1).Shapes

'Shape should contain the text starting with "Revenue"
If Left(MyShape.TextFrame.TextRange.Text, 7) = "Revenue" Then

'Change the text in the shape
MyShape.TextFrame.TextRange.Text = "Sales " & ShapeIndex
End If

'Increase the shape index for next shape
ShapeIndex = ShapeIndex + 1

Next MyShape
End Sub

 

We can see in the image below that before executing the VBA code, rectangular shapes had text as “Revenue 2017”, “Revenue 2018”, “Revenue 2019”.

 

add text to shape in slide powerpoint vba

 

Once we had executed the VBA code, text inside rectangular shapes have changed to “Sales 2017”, “Sales 2018”, “Sales 2019”.We have changed the text for each rectangle in the format of “Sales yyyy”, e.g. “Sales 2017” for the first box. Similarly, we have changed the text for other 2 boxes.

 

add text to shape in slide powerpoint vba

 

Reference

https://docs.microsoft.com/en-us/office/vba/api/powerpoint.shape.textframe


Post you may like

Apply count function in a cell in VBA Excel