PDA

View Full Version : [SOLVED:] Deleting all shapes with the same name with one click



RandomGerman
05-26-2015, 11:47 AM
Hi there,

I have built a bundle of macros creating shapes concerning the work status of my presentations, e.g., "To do", "Updated", "Work in progress", and defined a shape name ("Stickerxx") for all of them, because when I'm ready I want to get rid of them with one click for the whole presentation by using another macro.

The code for this deleting macro is:

Public Sub Callback006(control As IRibbonControl)
Dim sld As Slide
On Error Resume Next
For Each sld In ActivePresentation.Slides
sld.Shapes("Stickerxx").Delete
Next sld
End Sub


It works fine as long as there is only one of these shapes per slide. What do I have to add to delete a second one on the same slide at the same time? I guess it is quite a simple thing for experts, but as I am a beginner ...

John Wilson
05-27-2015, 03:32 AM
Allowing shapes to have the same name is a crazy MSFT "bug". Also as you have discovered only the first shape with that name is affected in code. To fix this you need to do this:


Public Sub Callback006(control As IRibbonControl)
Dim sld As Slide
Dim L As Long
On Error Resume Next
For Each sld In ActivePresentation.Slides
For L = sld.Shapes.Count To 1 Step -1
If sld.Shapes(L).Name = ("Stickerxx") Then sld.Shapes(L).Delete
Next L
Next sld
End Sub

RandomGerman
05-27-2015, 07:02 AM
Hi John,

thank you for this. Well, after beginning VBA coding just a few weeks ago just by using Google and asking questions in this forum, it might be not too surprising, that some of my ideas look crazy to experts. ;-) I started with creating static objects via macros, which wasn't too hard (except some details like indenting bullets or catching the right angle for a trapezoid), then moved to creating ribbons for these macros. Now I entered step three: Building macros that do something with selected objects. This seems to be far more advanced, so please excuse stupid questions or crazy ideas. Back to topic:

Of course your code works perfect for deleting all of my shapes with the same names. Do I understand right, that these same names actually are the problem for my second question (from the other Topic I opened on May 26): How to jump from one of these shapes to the next through the whole presentation? Using your code after just replacing "Delete" with "Select" doesn't help. But on the other hand - without giving names (or with giving different names) how can I define which sort of object I'd like to select or delete? Thank you for your patience with beginners.

John Wilson
05-27-2015, 07:14 AM
I was calling MSFT "crazy" not you!

It is part of your other problem but the real problem is that selecting a shape doesn't stop the code so it will (if "correct") just keep going to the end. It's actually quite difficult to do what you need

If I was you I would use the built in Find Next in the Home tab. Add some unusual text to each shape after your comment (e.g. zz) and find that.

RandomGerman
05-27-2015, 07:44 AM
I was calling MSFT "crazy" not you!

Oh, I thought, you meant my idea to use it for identification, not the possibility. And I didn't mind. ;-)



It is part of your other problem but the real problem is that selecting a shape doesn't stop the code so it will (if "correct") just keep going to the end. It's actually quite difficult to do what you need

Oh ... well, obviously I'm still so close to the beginning that I don't even have the right idea of what is difficult and what is not. Some things were a lot easier than I thought - this time it is the other way round.


If I was you I would use the built in Find Next in the Home tab. Add some unusual text to each shape after your comment (e.g. zz) and find that.

This is charming, but won't be a workaround to rely on as soon as I share the macros with friends, because they'd possibly change text in the objects and then delete the "zz" or whatever I used.


Ok, so far I will concentrate on other ideas (hopefully less difficult) and come back to this as soon as I learned more