Reputation: 629
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
.
Upvotes: 2
Views: 7417
Reputation: 2979
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.
Upvotes: 2
Reputation: 1323
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
Upvotes: 1
Reputation: 16311
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.
Upvotes: 3