Remove slides from presentation

I have version 17.x for asp.net and I am unable to remove a group of slides. The deck has 5 slides all numbered so I can tell which ones are getting removed. I am trying to remove slides 3 and 4. Slides 1, 2, 5 should remain. It’s skipping slides. I get 1, 2, 4? How do I get it to return my deck with slides 1, 2, 5?

Dim pres As Presentation = New Presentation(Path.Combine(Request.PhysicalApplicationPath, "Files\test.pptx))
Dim slide As ISlide = pres.Slides(2)
pres.Slides.Remove(slide)
slide = pres.Slides(3)
pres.Slides.Remove(slide)
pres.Save("deck.pptx", Export.SaveFormat.Pptx, Response, False)
Response.End()

@eformx3,

I have observed your sample code. Actually, when you remove slide 3 available at index 2 the slide collection gets shrinked by 1. Now, your slide collection has got 4 slides and your desired slide 4 will be moved to index 2 of collection instead of remaining at 3.

You need to call following statement for removing slide 4 as well.

slide = pres.Slides(2)

That works for a simple setup; but the slides to remove could change. I have the following that tries to pare down 38 slides from an array of slide numbers to remove. In the array are the slides to remove (Example: 5,6,7,8,9,10,13,14,15,16) How would I handle the removal of slides without knowing where things are being collapsed?

'remove slides where secondary is equal to the first item in our group of sales
For i As Integer = 0 To UBound(the_rules)

        'each the_rules record is comma separated, where the first value is the secondary style id

        '38 slides, remove 5 to 10, skip 11 and 12, remove 13 too 16
        Dim arr As Array = Split(the_rules(i), ", ", -1)

        If CInt(arr(0)) = the_secondary Then

            For a_slide As Integer = 1 To UBound(arr)
                Try
                    'get the slide number to remove, ex. 5
                    Dim r_slide As Integer = arr(a_slide)
                    pres.Slides.RemoveAt(r_slide)

                Catch ex As Exception
                    'error if no slide at this position
                    Dim s As String = ex.Message
                End Try

            Next

        End If

    Next

@eformx3,

I have observed your comments. Can you please share source presentation so that we may help you out further.

@eformx3,

I also suggest you that in your case with such a number of random slides deletion, you can copy the desired slides for removal in a custom ISlide list on your end first. This shall give a list of slide that you intend to remove. Then you traverse the slide collection and remove slide by calling Remove method and passing ISlide object from ISlide list for removal. This will ensure everything will be be done properly.