Randy
Randy

Reputation: 61

Is it possible to jump to a specific PowerPoint slide using VBA?

I have been trying to make a button on a slide that jumps to another slide that is named. I want to be able to do something like activeslide.view.slide ("Menu Slide")

That does not work, but I would think something like that should... Any help would be great!

Upvotes: 6

Views: 57205

Answers (2)

Binil
Binil

Reputation: 6583

try this code

a function to get the slide index by passing the name

Function GetSlideIndex(Slide As String) As Integer
    Dim retVal As Integer
    retVal = 0
    For i = 1 To ActivePresentation.Slides.Count
        If ActivePresentation.Slides(i).Name = Slide Then
            retVal = i
            Exit For
        End If
    Next
    GetSlideIndex = retVal
End Function

here used a CommandButton to navigate to the slide

Private Sub CommandButton1_Click()
    SlideShowWindows(1).View.GotoSlide GetSlideIndex("Slide2"), 1
End Sub

Upvotes: 4

asp8811
asp8811

Reputation: 803

First, you will need to actually name your slides. The slide's ".Name" property is different from and not connected to its Name in your outline. I'm only saying this because a lot of people do not realize it. You must set this property through VBA. If you don't do this, you can get some unexpected results. PowerPoint will name your slide "Slide#" wherever it was inserted, so if you insert your slide in the middle of your presentation, you can have multiple slides with the same name. If you are looking for a specifically named slide and have not renamed your slides, PowerPoint will return the first "Slide#" it finds in whatever loop you use to cycle through the Slides collection. If you edit your presentation and move slides around, this can give you lots of trouble. I would suggest renaming any slides that you know you will want to link to later (or writing something that would loop through the entire slides collection and change the ".Name" property of each slide to its Title 1 object contents).

Here is some code to rename the current slide

Sub ChangeSlideName()

    Dim NewName As String
    Dim ActiveSlide As Slide

    Set ActiveSlide = ActiveWindow.View.Slide

    NewName = InputBox("Enter new slide name for slide " & _
              ActiveSlide.SlideIndex, "New Slide Name", ActiveSlide.Name)

    If NewName = "" Then

        Exit Sub

    End If

    ActiveSlide.Name = NewName

End Sub


Here is some code to get the slide's index number. It works like the other answer, just a little more directly.

Function GetSlideIndex(SlideName As String)

    For Each Slide In ActivePresentation.Slides

        If Slide.Name = SlideName Then

            GetSlideIndex = Slide.SlideIndex
            Exit Function

        End If

    Next

End Function


Here is the sub that will actually send you to your slide

Sub MoveToSlide()

    SlideShowWindows(1).View.GotoSlide GetSlideIndex("YourSlideName")

End Sub

Edit to add: The MoveToSlide sub can be added to the Mouse Click or Mouse Over Actions for any object, not just command buttons. To do this, select the object you want to use and go to Insert -> Links -> Action and select Run Macro.

Upvotes: 7

Related Questions