Link to home
Start Free TrialLog in
Avatar of Jon Bredensteiner
Jon BredensteinerFlag for United States of America

asked on

How can one control the start/stop/close actions of PowerPoint presentations from Excel or from another presentation?

This is a follow-on question from: Using PowerPoint VBA to assign [Alt-Tab] to an action button

I am trying to use VBA in Excel to control PowerPoint presentations.  The below code was provided by Jamie Garroch in response to a similar question.  The procedure [StartSlideShow] works to open a presentation in Slide Show mode, but the procedure [ExitSlideShow] is not available to run as a macro.

Option Explicit

' Macros for opening a presentation, starting and stopping a slide show
' Runs from any VBE, including Excel (uses late binding)

' Define late bound objects for PowerPoint application instance and presentation
Public appPPT As Object
Public objPres As Object

' Start a slide show
' Set FilePathAndName to the presentation file to be opened
' Assign macro to relevant action button
Public Sub StartSlideShow()
  Dim FilePathAndName As String
  FilePathAndName = "c:\temp\test.pptx"
  ' Create an instance of PowerPoint
  Set appPPT = CreateObject("PowerPoint.Application")
  ' Open the PowerPoint file specified by the string variable FilePathAndName
  Set objPres = appPPT.Presentations.Open(FilePathAndName, ReadOnly:=msoFalse, Untitled:=msoFalse, WithWindow:=msoFalse)
  ' Run the slide show
  objPres.SlideShowSettings.Run
End Sub

' Exit the specified Slide Show or the first one if none specified
' Assign macro to relevant action button
Public Sub ExitSlideShow(Optional objSSW As Object)
  If objSSW Is Nothing Then
    appPPT.SlideShowWindows(1).View.exit
  Else
    objSSW.View.exit
  End If
End Sub

' Tidy Up (run when closing Excel VBA project)
Public Sub Tidy()
  Set objPres = Nothing
  Set appPPT = Nothing
End Sub

Open in new window


If I change the [ExitSlideShow] procedure from the above code to the following then it works, but I believe it is now just closing the first presentation, instead of closing the file specified in [StartSlideShow], and this won't work, because I will be opening multiple presentations at a time.

' Exit the specified Slide Show or the first one if none specified
' Assign macro to relevant action button
Public Sub ExitSlideShow()
  appPPT.SlideShowWindows(1).View.Exit
End Sub

Open in new window

SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You are not likely to have more that one slide SHOW running so it's not clear what you mean
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jon Bredensteiner

ASKER

I only selected my comment as the solution, because I wanted to post the correct code in case other people find it useful, but Rgonzo1971 wrote 99% of the code, and he received 100% of the points.