Delete table on PowerPoint Slide – VBA Excel

Objective

To delete table on PowerPoint slide using VBA Excel.

Approach

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.

Steps

  1. Set the required library reference as shown in the image below.
  2. Open the PowerPoint presentation from local drive
  3. Loop through each shape in slide, and check if the selected shape is a table or not
  4. If the shape is of type “msoTable”, then delete it
  5. If shape is not a table, then move to next slide
  6. Save the opened PowerPoint Presentation, and then close it


Code

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.

 

Delete Table Slide VBA Excel

 

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.

 

Delete Table Slide VBA Excel

 


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.

 

Delete Table Slide VBA Excel

 

As we can see below, after executing the code, both the tables have been deleted, while the chart remains intact.

 

Delete Table Slide VBA Excel

 

Note

  1. We can delete other type of objects using similar approach, e.g. for charts, auto shapes
  2. If needed we can also open the PowerPoint presentation using a dialog box.
  3. We can also loop through multiple or specific slides to delete the objects.

Post you may like

How to save PowerPoint Presentation using VBA Excel ?