PDA

View Full Version : [SOLVED:] Delete slide depending on status of checkbox control? This code is missing the mark..



ajjava
06-07-2019, 10:01 AM
I have a multi-slide presentation. Users are to go through and tick the checkbox control, for any slides they wish to "keep". I'd like to run a macro to then delete any slide that ISN'T checked. I feel like this code should work, but it's giving me weird results. It deletes slides, but randomly and only if I run it over and over. I'm aware that the checkbox is only "interactive" while in slideshow mode...that also appears to be the case for running macros that interact with checkboxes.


Sub DeleteSlides()

Dim sldTemp As Slide
Dim shpTemp As Shape


For Each sldTemp In ActivePresentation.Slides
For Each shpTemp In sldTemp.Shapes
If shpTemp.Type = msoOLEControlObject Then
If TypeName(shpTemp.OLEFormat.Object) = "CheckBox1" Then
If shpTemp.OLEFormat.Object.Value = False Then
sldTemp.Delete
End If
End If
End If
Next
Next


End Sub

John Wilson
06-07-2019, 02:11 PM
Try using "CheckBox" as the TypeName (Not CheckBox1)

ajjava
06-07-2019, 03:15 PM
Unfortunately, I already tried that. Still nothing.

Paul_Hossler
06-07-2019, 06:49 PM
I've always found that it's better to delete from the end going to the beginning




Option Explicit

Sub DeleteSlides()

Dim i As Long
Dim sldTemp As Slide
Dim shpTemp As Shape

For i = ActivePresentation.Slides.Count To 1 Step -1
Set sldTemp = ActivePresentation.Slides(i)
For Each shpTemp In sldTemp.Shapes
If shpTemp.Type = msoOLEControlObject Then
If TypeName(shpTemp.OLEFormat.Object) = "CheckBox1" Then
If shpTemp.OLEFormat.Object.Value = False Then sldTemp.Delete
End If
End If
Next
Next i

End Sub

John Wilson
06-08-2019, 05:40 AM
Paul is absolutely correct you should loop in reverse if you delete things.

However the Typename of a CheckBox is "CheckBox" so it should work. Are you sure you were careful with Case - It is case sensitive and has to be "CheckBox" You can also check the ProgID though


Sub DeleteSlides()

Dim i As Long
Dim sldTemp As Slide
Dim shpTemp As Shape


For i = ActivePresentation.Slides.Count To 1 Step -1
Set sldTemp = ActivePresentation.Slides(i)
For Each shpTemp In sldTemp.Shapes
If shpTemp.Type = msoOLEControlObject Then
If shpTemp.OLEFormat.ProgID = "Forms.CheckBox.1" Then
If shpTemp.OLEFormat.Object.Value = False Then sldTemp.Delete
End If
End If
Next
Next i


End Sub

ajjava
06-10-2019, 12:35 PM
Hot damn...that did the trick!
Out of curiosity, can you explain about the deleting in reverse? I've seen that mentioned a few times, but it doesn't sink it because I don't understand the basis for it.
Thanks for the help!!!

John Wilson
06-10-2019, 12:44 PM
I'll try

Suppose you have 4 slides and the target to delete is on slide 3

You create a loop like For counter= 1 to 4

to look at the slides

When it gets to slide 3 - "Yay found the one delete it"

Next I'll look at slide 4

Hang on - "Where's slide 4 - there's only 3 now."

ERROR

Work through the same logic going backwards ..

ajjava
06-10-2019, 01:24 PM
OMG, that makes perfect sense. THANK YOU!! Of course - it's so obvious now.