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

Jon BredensteinerProject ManagerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

pls try

Public Sub ExitSlideShow()
   For Each ssw in appPPT.SlideShowWindows
      If ssw.Presentation.FullName = "c:\temp\test.pptx" Then
         ssw.View.Exit
         Exit For
      End If
   Next
End Sub

Open in new window

Regards
JSRWilsonCommented:
You are not likely to have more that one slide SHOW running so it's not clear what you mean
Jon BredensteinerProject ManagerAuthor Commented:
JSRWilson,
The user would like to run four PowerPoint files from the Excel file, so there will be more than one open at a time.

Rgonzo1971,
Thank you for the code.  I tried the code you posted, but it throws an error because the variable "ssw" is not defined.  So I changed it just a little to the below code, and it worked.  Thank you for you help :)

Public objSSW As Object

Public Sub ExitSlideShow()
   For Each objSSW In appPPT.SlideShowWindows
      If objSSW.Presentation.FullName = "c:\temp\test.pptx" Then
         objSSW.View.exit
         Exit For
      End If
   Next
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jon BredensteinerProject ManagerAuthor Commented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft PowerPoint

From novice to tech pro — start learning today.