To add auto shapes in a PowerPoint slide using VBA while taking the input from user.
Here we have used two command buttons, first one for adding the shapes on the slide and second one for deleting the shapes from the slides. In this post we shall mention the details only about adding the shapes on a slide.
- Created a command button on the slide 1 and change the caption to Add Shapes.
- In code for adding shape, first we showed an input box which will ask for number of shapes to be created
- If no input is given then the no input box will be created, and code execution will exit the subroutine.
- Run the loop to add the auto shape in the slide number 1.
- Variable LocationOffset is used to avoid the shapes overlapping each other.
As we can see below that we have 2 command buttons, one for adding shapes and another one for deleting shapes.We shall get the user input by input box in VBA. Here we are giving 4 as input.
Once we click on “Add Shapes” button , 4 text boxes will be created, since we had given 4 as input in input box.
Private Sub AddShapeButton_Click() Dim oNewShp As Shape Dim NoOfShapes As String Dim LocationOffset As Integer 'Variable for changing the shape distance from top LocationOffset = 0 'User input for shapes NoOfShapes = InputBox("Enter the number of shapes to be created") If NoOfShapes = vbNullString Then MsgBox ("No input given for count of shapes") Exit Sub End If 'Running the loop for creating shapes in slide 1 For i = 1 To NoOfShapes Set oShp = ActivePresentation.Slides(1).Shapes.AddShape(Type:=msoAutoShape, Left:=60, Top:=80 + LocationOffset, Width:=120, Height:=40) LocationOffset = LocationOffset + 50 Next i End Sub
We can also add other PowerPoint objects like charts, text boxes etc. using the same approach.
Post you may like