Mac – How to run a macro whenever I enter a new slide

macrosmicrosoft-powerpoint

I'd like to have a simple macro run whenever I enter a new slide, whether I got there by normal click-to-advance or by following a link within the slideshow. Is this possible? I've searched for some kind of "page" or "slide" object upon which I could add an "enter" or "load" action, but if it exists I have not found it yet.

The goal is to set a "previous slide" global var, which is used to implement a Back button. Note that this button returns the user to the last slide he or she was on, not necessarily the previous slide in order. That is, on slide #4 I click a link to slide #101 in the appendix, then click Back and I go back to #4, not #100. This works fine, I just need to know which slide I was at.

As a workaround, I'm adding an invisible object to the slide master, with a mouse-over action to call this macro. This should work, but it seems ugly.

Is there a better way? Thank you!


Here's the code which worked:

 Public PreviousSlideIndex As Long
 Public CurrentSlideIndex As Long

 Public Sub OnSlideShowPageChange(ByVal Window As SlideShowWindow)
      PreviousSlideIndex = CurrentSlideIndex
      CurrentSlideIndex = ActivePresentation.SlideShowWindow.View.Slide.SlideIndex
 End Sub

 Sub ReturnToPreviousSlide()
      ActivePresentation.SlideShowWindow.View.GoToSlide PreviousSlideIndex
 End Sub

I set ReturnToPreviousSlide to the OnClick action for my Back button, and all's good.

You could make this a little sophisticated with a stack of previous pages, allowing users to futz around for a few slides and still return from whence they came.

Best Answer

I believe you're looking for the SlideShowNextSlide event, as documented at http://msdn.microsoft.com/en-us/library/ff745863.aspx:

Occurs immediately before the transition to the next slide. For the first slide, occurs immediately after the SlideShowBegin event.

An example is included at the MSDN link.

Per http://officeone.mvps.org/vba/run_macro_at_slide.html ("Run VBA macro at a particular slide"), there is also a OnSlideShowPageChange event, though I've not been able to find any official documentation for this on MDSN. However, per http://officeone.mvps.org/vba/events_version.html:

Occurs after showing the new slide.

This is an auto-macro and does not require any object to be declared. It is fired even when the presentation that hosts these event handlers is not in slide show mode.

Related Question