Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

powerpoint

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!

like image 564
Randy Avatar asked Jan 27 '11 16:01

Randy


People also ask

How do I go directly to a specific slide in PowerPoint?

Go to a specific slide Mouse: Right-click a slide, select Go to Slide on the shortcut menu, type the slide number, and then click OK.

How do you jump to a slide with an action button in PowerPoint?

To choose the action that takes place when you click or move the pointer over the action button, do one of the following: Choose Hyperlink to and then select the destination (for example, the next slide, the previous slide, the last slide, or another PowerPoint presentation) that you want the hyperlink action to go to.


2 Answers

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.

like image 199
asp8811 Avatar answered Dec 28 '22 09:12

asp8811


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
like image 28
Binil Avatar answered Dec 28 '22 07:12

Binil