dennis96411
dennis96411

Reputation: 127

VBA PowerPoint - Go back to the slide I came from

How would I accomplish this? Because I'm constantly adding and deleting the slides, I need the code to KNOW what slide I came from and not just go to a set slide.

Upvotes: 1

Views: 7672

Answers (4)

Phil
Phil

Reputation: 1

[Microsoft 365] Within a presentation (slide show), type the number and press Enter. When editing the slides, I use this VBA macro kept in a file "Macros.pptm":

Sub GoToSlide()
Dim currNum As Integer, newNum As Integer, a As String
currNum = _ 
CInt(Application.ActiveWindow.View.Slide.SlideIndex)
a = InputBox("Current slide [" & currNum & "]:" & _ 
vbCrLf & _"Go to slide number:", _
"GoToSlide", currNum)
If (a <> "") Then
newNum = CInt(a)
Application.ActiveWindow.View.GoToSlide (newNum)
End If
End Sub

I have assigned this macro as a new command to the quick access ribbon. When I edit a deck I have to open the file "Macro.pptm" too in order to find the macro. The macro displays the current slide number and prompts for the desired slide number, then goes there (unless user presses ESC).

Upvotes: 0

Steve Rindsberg
Steve Rindsberg

Reputation: 51

If you're using a button anyhow, why not use PPT's action settings; assign an action setting of Previously Viewed Slide. Clicking the button will take the viewer back to whatever slide they were viewing immediately prior to the current slide.

No VBA required.

Upvotes: 0

pinkfloydx33
pinkfloydx33

Reputation: 12739

 SlideShowWindows(1).View.GotoSlide(SlideShowWindows(1).View.LastSlideViewed.SlideIndex)

Upvotes: 5

dennis96411
dennis96411

Reputation: 127

I figured it out. With some help from browsing the web and a bit of thinking, I came up with this:

For storing the current slide number except the slides that requires you to go back to another slide, I made a module with:

Public CurrentSlide As Long, PreviousSlide As Long

Public Sub GoToPreviousSlide()
SlideShowWindows(1).View.GoToSlide (PreviousSlide)
End Sub

Public Sub StoreCurrentSlide()
CurrentSlide = ActivePresentation.SlideShowWindow.View.Slide.SlideIndex
If CurrentSlide <> 14 Then
'Execute only if the current slide is not a slide that uses this function (because if you store the current slide while in a slide that requires you to go back to another slide, it will overwrite the PurrentSlide value and render this useless). Add more slide numbers to the If statement as you need them.
PreviousSlide = CurrentSlide
Call GoToPreviousSlide
End If
End Sub

Of course I'd have to make sure that I call StoreCurrentSlide every time the slide changes, I made another module to automatically execute this:

Public Sub OnSlideShowPageChange()
StoreCurrentSlide
End Sub

Now I can use a button anywhere and go back to the previous slide by using:

Private Sub OK_Click()
GoToPreviousSlide
End Sub

Upvotes: 0

Related Questions