To delete table on PowerPoint slide using VBA Excel.
Here we are opening a PowerPoint presentation from a local drive, and thereafter we are looping through each shape on slide 1. If the selected shape is a table, then that shape is deleted. If selected shape is not a table, then the code execution moves to next shape. The VBA code is written in .xlsm file.
- Set the required library reference as shown in the image below.
- Open the PowerPoint presentation from local drive
- Loop through each shape in slide, and check if the selected shape is a table or not
- If the shape is of type “msoTable”, then delete it
- If shape is not a table, then move to next slide
- Save the opened PowerPoint Presentation, and then close it
Sub DeleteTableOnPowerPointSlide() Application.ScreenUpdating = False Dim TargetPpt As PowerPoint.Presentation Dim ObjShp As PowerPoint.Shape Dim TotalShapesOnSlide As PowerPoint.Shapes Dim ObjPowerPointApp As PowerPoint.Application Set ObjPowerPointApp = CreateObject("PowerPoint.Application") 'Open PPT from local drive Set TargetPpt = ObjPowerPointApp.Presentations.Open("C:\Users\yourusername\Desktop\test\Mysampleppt.pptx") Set TotalShapesOnSlide = ObjPowerPointApp.ActivePresentation.Slides(1).Shapes shpNumber = 1 'Looping through each shape on slide by using shape index For shpcount = 1 To TotalShapesOnSlide.Count If ObjPowerPointApp.ActivePresentation.Slides(1).Shapes(shpNumber).Type = msoTable Then ObjPowerPointApp.ActivePresentation.Slides(1).Shapes(shpNumber).Delete Else shpNumber = shpNumber + 1 End If Next shpcount 'Save and close the opened ppt ObjPowerPointApp.ActivePresentation.Save ObjPowerPointApp.Windows(1).Close End Sub
First step is to set the library reference as shown below.
We have allocated the macro code to a command button in the macro file(xlsm). Once we click on this button, the VBA code executes.
In the image below we can see that there were 2 tables and 1 chart in the slide before deleting the tables using VBA. We want to delete only tables; no other PowerPoint object should be deleted from slide.
As we can see below, after executing the code, both the tables have been deleted, while the chart remains intact.
- We can delete other type of objects using similar approach, e.g. for charts, auto shapes
- If needed we can also open the PowerPoint presentation using a dialog box.
- We can also loop through multiple or specific slides to delete the objects.
Post you may like