VBA Code to Stop a PowerPoint Slide Show

KopeNor

New Member
Joined
Nov 18, 2016
Messages
1
Hello all,

I currently have an Excel spreadsheet that uses a timer to automatically runs code that populates two charts, copy and pastes those charts as pictures into new slides in a PowerPoint presentation, and then starts a slide show.

The issue I'm having is that when the timer triggers the code to run again, I get an error (Run-time error, Application: Invalid request) because the slide show is still running. Is there a way that I can use some kind of if/then statement to find out if a slide show is currently running then exit out of the slide show?

Below is the code of the module that is giving the error.

Thanks for any help. This site has been immensely helpful with all of my VBA problems.


Code:
Sub ChartToPresentation()
' Uses Early Binding to the PowerPoint Object Model
' Set a VBE reference to Microsoft PowerPoint Object Library


Dim PPApp As PowerPoint.Application
Dim PPPres As PowerPoint.Presentation
Dim PPSlide As PowerPoint.Slide
Dim SlideCount As Long
Dim iCht As Integer
Dim x As Long


' Reference existing instance of PowerPoint
Set PPApp = GetObject(, "Powerpoint.Application")
' Reference active presentation
Set PPPres = PPApp.ActivePresentation


' ****************************************
' An error occurs at the next line of code
' ****************************************


PPApp.ActiveWindow.ViewType = ppViewSlide


If PPPres.Slides.Count <> 0 Then
    For x = PPPres.Slides.Count To 1 Step -1
        PPPres.Slides(x).Delete
    Next x
End If


For iCht = 1 To ActiveSheet.ChartObjects.Count
    ' copy chart as a picture
    ActiveSheet.ChartObjects(iCht).Chart.CopyPicture _
    Appearance:=xlScreen, Size:=xlScreen, Format:=xlPicture


    ' Add a new slide and paste in the chart
    SlideCount = PPPres.Slides.Count
    Set PPSlide = PPPres.Slides.Add(SlideCount + 1, ppLayoutBlank)
    PPApp.ActiveWindow.View.GotoSlide PPSlide.SlideIndex
    With PPSlide
        ' paste and select the chart picture
        .Shapes.Paste.Select
        ' align the chart
        PPApp.ActiveWindow.Selection.ShapeRange.Width = 720
        PPApp.ActiveWindow.Selection.ShapeRange.Align msoAlignCenters, True
        PPApp.ActiveWindow.Selection.ShapeRange.Align msoAlignMiddles, True
    End With
    With PPSlide.SlideShowTransition
        .Speed = ppTransitionSpeedSlow
        .EntryEffect = ppEffectFadeSmoothly
        .AdvanceOnTime = msoTrue
        .AdvanceTime = 12
    End With
Next


PPPres.SlideShowSettings.Run


' Clean up
Set PPSlide = Nothing
Set PPPres = Nothing
Set PPApp = Nothing


End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.

Forum statistics

Threads
1,215,012
Messages
6,122,682
Members
449,091
Latest member
peppernaut

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top