Delete Named Shapes/Objects via Excel VBA

NinaE_11

Board Regular
Joined
Aug 18, 2020
Messages
54
Office Version
  1. 2016
Platform
  1. Windows
Hello!

I'm looking to delete specific named objects and shapes in an existing PowerPoint presentation via VBA in Excel. I have identified each shape by a name, however, I cannot figure out how to utilize the exact name in VBA language that will clear the contents of these shapes. I do not need to loop through each slide in PPT; I would prefer to write code to go to a specific slide, select the shape and then delete it using the assigned name. However, all I can find online is how to loop through an entire presentation - yet, I can't even get that to work. I want to delete one named shape - how do I get this to work?

Please see simple code below - any guidance is much appreciated, thank you!

Sub ClearContents()

Dim papp As PowerPoint.Application
Dim ppt As PowerPoint.Presentation

Dim shp As Shape
Dim sld As slide

Set papp = New PowerPoint.Application
Set ppt = papp.Presentations.Open ("Path")

papp.Activate

papp.ActiveWindow.View.GotoSlide (3)
sld(3).shp("NINA").delete

End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Try the following...

VBA Code:
Sub ClearContents()

    Dim papp As PowerPoint.Application
    Dim ppt As PowerPoint.Presentation
    Dim sld As PowerPoint.Slide
    Dim shp As PowerPoint.Shape
   
    Set papp = New PowerPoint.Application
    Set ppt = papp.Presentations.Open("Path")
    Set sld = ppt.Slides(3)
    Set shp = sld.Shapes("NINA")
   
    shp.Delete

End Sub

And, of course, if you really want to go to that particular slide, you can add...

Code:
papp.ActiveWindow.View.GotoSlide 3

Or, since you've assigned that slide to sld...

Code:
papp.ActiveWindow.View.GotoSlide sld.SlideIndex

Hope this helps!
 
Upvote 0
Solution
Try the following...

VBA Code:
Sub ClearContents()

    Dim papp As PowerPoint.Application
    Dim ppt As PowerPoint.Presentation
    Dim sld As PowerPoint.Slide
    Dim shp As PowerPoint.Shape
  
    Set papp = New PowerPoint.Application
    Set ppt = papp.Presentations.Open("Path")
    Set sld = ppt.Slides(3)
    Set shp = sld.Shapes("NINA")
  
    shp.Delete

End Sub

And, of course, if you really want to go to that particular slide, you can add...

Code:
papp.ActiveWindow.View.GotoSlide 3

Or, since you've assigned that slide to sld...

Code:
papp.ActiveWindow.View.GotoSlide sld.SlideIndex

Hope this helps!
Wow, thank you very much! Worked perfectly, and thank you for the speedy response.
 
Upvote 0

Forum statistics

Threads
1,214,644
Messages
6,120,709
Members
448,983
Latest member
Joaquim_Baptista

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top