Add text boxes on a PowerPoint slide using VBA

Insert text boxes on a slide using VBA

Objective

Adding the text boxes on a PowerPoint slide using VBA.

Approach

In the code below we are trying to create multiple text boxes on a PowerPoint slide using VBA code. Number of text boxes which can be created are dynamic. Herein we get the user input by an input box, and then, based upon the input, we run the loop to create the text boxes. We have declared a shape variable “shp” which refers to the added text box. We are adding a text box using AddTextBox method.

Steps

Below shown are the functionalities which will be obtained by this code –

  1. Add dynamic number of text boxes
  2. Add some text in the text box
  3. Modify the font size
  4. Change the font color and make the font style bold
  5. Add border to text box

The slide on which we have created the text boxes have 2 command buttons, one for adding text box and other one for deletion. But same code can be written in a module as well.Below shown code is assigned to add button on the slide. Once we click on the “Add Text Box”, input box will appear. We shall input the number of boxes to be created and the same number of text boxes will be created.

Private Sub CommandButton1_Click()
Dim shp As Shape
Dim shpCount As Integer
Dim offset_height As Integer
shpCount = InputBox("Enter the number of text boxes to be created")
offset_height = 0
For i = 1 To shpCount
Set shp = ActivePresentation.Slides(1).Shapes.AddTextBox(Orientation:=msoTextOrientationHorizontal, _
Left:=100, Top:=100 + offset_height, Width:=180, Height:=20)
shp.TextFrame.TextRange.Text = "Sample Text Box " & i
shp.TextFrame.TextRange.Font.Size = 13
shp.TextFrame.TextRange.Font.Bold = msoTrue
shp.TextFrame.TextRange.Font.Color = RGB(0, 0, 255)
shp.TextFrame.TextRange.ParagraphFormat.Alignment = ppAlignCenter
shp.Line.DashStyle = msoLineSolid
offset_height = offset_height + 30
Next i
End Sub

In the image below you can see that we have 2 command buttons, once we click on “Add Text Box” command button, an input box will pop up.We shall add number of text boxes which we want to create.Here we have entered 3 as input.

Insert Image in slide VBA

In the image below we can see that 3 text boxes have been created as a result of input provided earlier.

Insert Image in slide VBA

Note:

We can also customize the same code to apply it for images, shapes or any other power point objects. 

Reference

https://docs.microsoft.com/en-us/office/vba/api/powerpoint.shapes.addtextbox

Post you may like

Delete all the text boxes on a slide using VBA