views:

35

answers:

1

I am trying to programmatically copy a slide in a PowerPoint presentation, and paste it right after the original.

My first thought was to get the index of the old slide, and add the copy at the desired new index, but I can't seem to find a straightforward way to retrieve that index. I expected to have something like Slides.IndexOf(Slide slide), but couldn't find anything like that. I ended up writing the very old-school following code, which seems to work, but I am curious as to whether there is a better way to do this.

var slide = (PowerPoint.Slide)powerpoint.ActiveWindow.View.Slide;
var slideIndex = 0;
for (int index = 1; index <= presentation.Slides.Count; index++)
{
    if (presentation.Slides[index] == slide)
    {
        slideIndex = index;
        break;
    }
}

This is C#/VSTO, but any input that could put me on the right path is appreciated, be it VBA or VB!

+2  A: 

Yes, what you want is Duplicate which returns a SlideRange. Here's an example in VBA:

Sub DuplicateSlide()
    Dim ap As Presentation
    Set ap = ActivePresentation
    Dim sl As SlideRange
    Set sl = ap.Slides(2).Duplicate
End Sub

To just get the slide's index, you can use this:

Sub GetSlideIndex()
    Dim ap As Presentation
    Set ap = ActivePresentation
    Set sl = ap.Slides(2)
    Debug.Print sl.SlideIndex
End Sub
Otaku
Unless I missed something, it doesn't directly answer my question, because what I am after is how to get the index of a specific slide. That being said, thanks for the Duplicate method, which will come handy!
Mathias
@Mathias: I thought you wanted a technique to copy and paste a slide right after the original. But I've updated with the way to get the Slide index.
Otaku
Thank you! I knew there had to be a way to get that index, I was expecting it to be a method on Slides, not a property of the Slide itself. And I appreciate the tip on Duplication, which will come very handy.
Mathias