Consulting

Results 1 to 8 of 8

Thread: Delete slide depending on status of checkbox control? This code is missing the mark..

  1. #1
    VBAX Regular
    Joined
    Mar 2019
    Posts
    57
    Location

    Delete slide depending on status of checkbox control? This code is missing the mark..

    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

  2. #2
    VBAX Master
    Joined
    Feb 2007
    Posts
    1,804
    Location
    Try using "CheckBox" as the TypeName (Not CheckBox1)
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  3. #3
    VBAX Regular
    Joined
    Mar 2019
    Posts
    57
    Location
    Unfortunately, I already tried that. Still nothing.

  4. #4
    VBAX Wizard
    Joined
    Apr 2007
    Posts
    6,419
    Location
    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
    
    Paul

    ------------------------------------------------------------------------------------------------------------------------
    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s)
    (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    VBAX Master
    Joined
    Feb 2007
    Posts
    1,804
    Location
    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
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  6. #6
    VBAX Regular
    Joined
    Mar 2019
    Posts
    57
    Location
    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!!!

  7. #7
    VBAX Master
    Joined
    Feb 2007
    Posts
    1,804
    Location
    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 ..
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  8. #8
    VBAX Regular
    Joined
    Mar 2019
    Posts
    57
    Location
    OMG, that makes perfect sense. THANK YOU!! Of course - it's so obvious now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •