2

I have scoured MSDN and the web for how to use VBA to select a range of slides in PowerPoint and cannot figure it out. It seems like it should have something to do with ActiveWindow.Selection.SlideRange, but I cannot figure out how to set a starting and ending slide. All I can find is how to use VBA to manipulate a range of slides that the user has already selected; I need to make VBA select the range.

Specifically, I want to select the slide before and the slide after the currently selected slide and return those 3 slides as the SlideRange.

3 Answers 3

3

You can get the active slide index by using:

Dim intIndex As Long
intIndex = ActiveWindow.View.Slide.SlideIndex

Then, use the Slides.Range() function to select the slides before and after this index:

Dim r As SlideRange
Set r = ActivePresentation.Slides.Range(Array(intIndex - 1, intIndex, intIndex + 1))
r.Select

You may want to protect against indexes < 1 or greater than the number of slides in your presentation. For example, if the first slide is selected before you run the macro, then there won't be a slide 0 and trying to select it will result in an error.

3
  • Ahhh! Use the Array(item1, item2....) function inside the Range property to identify the items I want to select. Brilliant. That is the piece I was missing. Thank you!
    – DRC
    Aug 26, 2015 at 0:57
  • Happy to help. Check this out for more info on Slides.Range().
    – Bond
    Aug 26, 2015 at 0:58
  • It is absolutely critical that the thumbnails view is active when you run this code, or only the last slide in the array will be selected. You should call ActiveWindow.Panes(1).Activate before you create the SlideRangeand call Select on it. Dec 29, 2019 at 23:05
1

An example of how to select a slide before and after. Then this selection is applied format.

Sub ExampleSlideRange()
    Dim index, indexB, indexA, count As Integer
    Dim sr As SlideRange
    index = ActiveWindow.View.Slide.SlideIndex
    count = ActivePresentation.Slides.count
    indexB = index - 1
    indexA = index + 1
    If indexB = 0 Then indexB = 1
    If indexA > count Then indexA = count        
    Set sr = ActivePresentation.Slides.Range(Array(indexB, index, indexA))
    sr.Select
    With Windows(1).Selection.SlideRange
        .FollowMasterBackground = False
        .Background.Fill.PresetGradient msoGradientHorizontal, 1, msoGradientLateSunset
    End With
End Sub
1

One minor modification to the answers above is that you cannot select multiple slides if the corresponding Pane is not active (you CAN set a reference to them but the Select method implies a UI action). So, in the Normal view, if the slide or notes pane is active then only the last slide in the range is selected, being reflected in the thumbnail pane. To set a multi-slide range AND and see them selected in the thumbnail pane, you need to activate that pane first as follows:

ActiveWindow.Panes(1).Activate
Set r = ActivePresentation.Slides.Range(Array(intIndex - 1, intIndex, intIndex + 1))
r.Select

Trying to activate panes in other views may cause an issue so you should also check the ActiveWindow.ViewType first.

Note too that the array can be an array of numerical slide indexes or an array of strings containing the slide name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.