How Can I Run a PowerPoint Slide Show From a Script?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I run a PowerPoint slide show from a script?

— IB

SpacerHey, Scripting Guy! AnswerScript Center

Hey, IB. Talk about vindication! A couple years ago, when the Scripting Guy who writes this column first started messing around with Microsoft Office, he wrote a simple little script that could run a PowerPoint slide show. The other Scripting Guys laughed at him. “That was a waste of time,” they said. “What use are you ever going to have for a script that runs a PowerPoint slide show?” Well, other Scripting Guys: who’s laughing now?

Well, OK, technically they’re all still laughing at him. And, come to think of it, they were all laughing at him before he wrote a script that could run a PowerPoint slide show. Hmmm, wonder if that means anything ….

While we ponder this situation feel free to read a magazine or go get a sandwich. Or, even better, take a gander at a script that can run a PowerPoint slide show:

On Error Resume Next

Const ppAdvanceOnTime = 2 Const ppShowTypeKiosk = 3 Const ppSlideShowDone = 5

Set objPPT = CreateObject(“PowerPoint.Application”) objPPT.Visible = True

Set objPresentation = objPPT.Presentations.Open(“C:\Scripts\Test.ppt”)

objPresentation.Slides.Range.SlideShowTransition.AdvanceOnTime = TRUE objPresentation.SlideShowSettings.AdvanceMode = ppAdvanceOnTime objPresentation.SlideShowSettings.ShowType = ppShowTypeKiosk

objPresentation.SlideShowSettings.StartingSlide = 1 objPresentation.SlideShowSettings.EndingSlide = objPresentation.Slides.Count

Set objSlideShow = objPresentation.SlideShowSettings.Run.View

Do Until objSlideShow.State = ppSlideShowDone If Err <> 0 Then Exit Do End If Loop objPresentation.Saved = True objPresentation.Close objPPT.Quit

This is a crazy-looking script, but there’s really not that much to it; for the most part all we’re doing is setting various property values. We’ll briefly run through the properties that we used in configuring the slide show, but these are by no means all the properties (nor have we used all the available options for these properties). For more information, see the Microsoft PowerPoint VBA Language Reference on MSDN.

We start out simply enough, defining three constants:

ppAdvanceOnTime. Setting this value to 2 causes the slide show to run according to preset timings. We could have configured the slide show so that it would advance only when the user clicked the mouse button, but we assumed that, if you want to automate the process of opening up the slide show, you probably want to automate the actual presentation of the slides as well.

ppShowTypeKiosk. Setting this value to 3 causes the slide show to display in “kiosk” mode (in which it fills the entire screen, including the desktop toolbar).

ppSlideShowDone. Setting this value to 5 causes the slide show to continue until every slide has been shown.

After defining the three constants we create an instance of the PowerPoint.Application object and then set the Visible property to True; we then use the Open method to open the file C:\Scripts\Test.ppt. With the presentation open we set several properties of the slide show, including AdvanceOnTime and AdvanceMode (which, taken together, cause the presentation to run automatically), and ShowType, which displays the presentation in kiosk mode. If you choose to type these values into your script (as opposed to copying and pasting) notice that you have to use two different objects to configure the three values.

Note. So what if you don’t want to use preconfigured slide timings? No problem; just add this property:

objPresentation.Slides.Range.SlideShowTransition.AdvanceTime = 2

The AdvanceTime property specifies the number of seconds each slide should appear on screen. Setting this value to 2 causes each slide to stay onscreen for two seconds before the presentation automatically moves on to the next slide. Incidentally, you can use this value to override any preconfigured slide timings.

That leaves us with just two properties – StartingSlide and EndingSlide – that we need to configure. (OK, these are actually optional, but we tossed them in because they’re good properties to know about.) We want to start the presentation with slide 1 and end the presentation with the last slide in the show; therefore we set the value of StartingSlide to 1 and the value of EndingSlide to the total number of slides in the show (objPresentation.Slides.Count):

objPresentation.SlideShowSettings.StartingSlide = 1
objPresentation.SlideShowSettings.EndingSlide = objPresentation.Slides.Count

What if we wanted to show just a portion of the show, say, slides 18-31? All you had to do was ask:

objPresentation.SlideShowSettings.RangeType = 2
objPresentation.SlideShowSettings.StartingSlide = 18
objPresentation.SlideShowSettings.EndingSlide = 31

As you can see, we simply specified the appropriate starting and ending slides. In addition to that, we also had to set the RangeType to 2; that tells PowerPoint that we’re using a specified range of slides as opposed to all the slides in the presentation.

After all the properties are configured we’re ready to run the slide show. To do that we first create an instance of the SlideShowSettings.Run.View object. That can be done with a single line of code:

Set objSlideShow = objPresentation.SlideShowSettings.Run.View

As soon as we have the View object we can set up a Do Loop that runs until the slide show has completed:

Do Until objSlideShow.State = ppSlideShowDone
    If Err <> 0 Then
        Exit Do
    End If
Loop

Don’t leave this block of code out; if you do, the presentation will momentarily flash onscreen, then just as quickly disappear. This little block of code keeps PowerPoint running – and visible – until the entire presentation has been viewed.

Oh, yeah: there is an If Then statement in the middle of the loop, isn’t there? Why? Well, suppose someone is watching your slide show and they press the Escape key on the keyboard. Assuming we left out the If Then statement that would do two things. First, it will create an error: the script is supposed to keep displaying slides until all the slides have been shown, only now the presentation has disappeared. That’s a problem, and an error occurs. Second, that would also drop the user into PowerPoint itself, a move that defeats the whole idea of an auto-run slide show.

Therefore, we use the If Then statement to constantly monitor the value of the VBScript Err object. If an error occurs (that is, if the value of Err does not equal 0) we immediately exit the Do loop. (There’s no point in staying in the loop if there aren’t any more slides to display.) In turn exiting out of the loop causes us to execute the last three lines in the script, lines that we will explain in just a second:

objPresentation.Saved = True
objPresentation.Close
objPPT.Quit

OK, so what does happen when an error occurs, or when the slide show finishes and we exit the Do loop at the end? Well, as we noted, we run those last three lines of code. In line 1 we set the Saved property to True; that simply assures PowerPoint that the presentation has been saved (it hasn’t, but PowerPoint will take our word for it), and keeps the application from popping up a dialog box asking if we want to save changes to the file. In the last two lines we close the file Test.ppt and then terminate PowerPoint. Sorry, show’s over, folks!

In other words, if a user tries to prematurely end the slide show (or when the slide show finishes as scheduled) the file gets closed, PowerPoint gets terminated, and the script ends.

Hope that helps, IB. And we hope you’ll agree that the Scripting Guy who writes this column is anything but crazy.

Well, OK, sure, he won’t get one of those super-saver cards at the grocery store, even though it would definitely save him money. But that’s about as crazy as he gets.

Other than the fact that, when he eats a sandwich he has to eat it from left-to-right; he can’t go from right-to-left or – heaven forbid! – take a bite right out of the middle. (It goes without saying that the peanut butter must be on the bottom slice and the jelly on the top slice; no upside-down sandwiches for him!) And he always has to sleep with his head facing the door. And if he’s watching TV and his team starts to lose he’ll get up and sit in a different chair. And ….

NOTE: The script in this post does not work with PowerPoint 2007. For a script that works with that version see the TechNet Script Center Gallery.

0 comments

Discussion is closed.

Feedback usabilla icon