André
André

Reputation: 3

VBA: Delete all slides (in PPT) except for specific ones

I am executing a code in through Excel and I would like it to delete all slides in my PPT except for slide 1, 2, 3 and 17. I can't seem to get it to work.

Here is a snip that deletes all slides in the presentation, how can the exception be implemented?

    For i = ppApp.ActivePresentation.Slides.Count To 2 Step -1
             ppApp.ActivePresentation.Slides(i).Delete
    Next

Upvotes: 0

Views: 2418

Answers (3)

Marco Vos
Marco Vos

Reputation: 2968

Try this:

Dim arrSheetsToKeep As Variant
arrSheetsToKeep = Array(1, 2, 3, 17)    

For i = ppApp.ActivePresentation.Slides.Count To 1 Step -1

    If IsError(Application.Match(i, arrSheetsToKeep, False)) Then

             ppApp.ActivePresentation.Slides(i).Delete

    End If        
Next

Just fill arrSheetsToKeep with the sheets you want to keep.

Upvotes: 1

FreeMan
FreeMan

Reputation: 5687

Give this a shot:

For i = ppApp.ActivePresentation.Slides.Count To 4 Step -1
  If I <> 17 then
    ppApp.ActivePresentation.Slides(i).Delete
  End If
Next

This will delete all but those 4 slides, no matter how many you may have started with. By stopping your loop at slide #4, you never even consider slides 1, 2 or 3, therefore you don't have to test to ensure you're not deleting them.

NOTE: When testing code that deletes things, always make sure you have a backup copy of what you're deleting from, just in case...

Upvotes: 0

Wouter
Wouter

Reputation: 383

Easist thing would be to move slide 17 to position 4 and then do following:

Do While ppApp.ActivePresentation.Slides.Count > 4
        ppApp.ActivePresentation.Slides(5).Delete
    Loop

Hope it helps

Upvotes: 1

Related Questions