{"id":66543,"date":"2006-09-05T12:16:00","date_gmt":"2006-09-05T12:16:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/09\/05\/how-can-i-run-a-powerpoint-slide-show-from-a-script\/"},"modified":"2006-09-05T12:16:00","modified_gmt":"2006-09-05T12:16:00","slug":"how-can-i-run-a-powerpoint-slide-show-from-a-script","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-run-a-powerpoint-slide-show-from-a-script\/","title":{"rendered":"How Can I Run a PowerPoint Slide Show From a Script?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I run a PowerPoint slide show from a script?<BR><BR>&#8212; IB<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>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. \u201cThat was a waste of time,\u201d they said. \u201cWhat use are you ever going to have for a script that runs a PowerPoint slide show?\u201d Well, other Scripting Guys: who\u2019s laughing now?<\/P>\n<P>Well, OK, technically they\u2019re all <I>still<\/I> laughing at him. And, come to think of it, they were all laughing at him <I>before<\/I> he wrote a script that could run a PowerPoint slide show. Hmmm, wonder if that means anything \u2026.<\/P>\n<P>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:<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>Const ppAdvanceOnTime = 2\nConst ppShowTypeKiosk = 3\nConst ppSlideShowDone = 5<\/p>\n<p>Set objPPT = CreateObject(&#8220;PowerPoint.Application&#8221;)\nobjPPT.Visible = True<\/p>\n<p>Set objPresentation = objPPT.Presentations.Open(&#8220;C:\\Scripts\\Test.ppt&#8221;)<\/p>\n<p>objPresentation.Slides.Range.SlideShowTransition.AdvanceOnTime = TRUE\nobjPresentation.SlideShowSettings.AdvanceMode = ppAdvanceOnTime \nobjPresentation.SlideShowSettings.ShowType = ppShowTypeKiosk<\/p>\n<p>objPresentation.SlideShowSettings.StartingSlide = 1\nobjPresentation.SlideShowSettings.EndingSlide = objPresentation.Slides.Count<\/p>\n<p>Set objSlideShow = objPresentation.SlideShowSettings.Run.View<\/p>\n<p>Do Until objSlideShow.State = ppSlideShowDone\n    If Err &lt;&gt; 0 Then\n        Exit Do\n    End If\nLoop\nobjPresentation.Saved = True\nobjPresentation.Close\nobjPPT.Quit\n<\/PRE>\n<P>This is a crazy-looking script, but there\u2019s really not that much to it; for the most part all we\u2019re doing is setting various property values. We\u2019ll briefly run through the properties that <I>we<\/I> 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 <A href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/vbapp11\/html\/WelcomePowerPointVBA_HV01135784.asp\" target=\"_blank\"><B>Microsoft PowerPoint VBA Language Reference<\/B><\/A> on MSDN.<\/P>\n<P>We start out simply enough, defining three constants:<\/P>\n<TABLE border=\"0\" cellSpacing=\"0\" cellPadding=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>ppAdvanceOnTime<\/B>. 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.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>ppShowTypeKiosk<\/B>. Setting this value to 3 causes the slide show to display in \u201ckiosk\u201d mode (in which it fills the entire screen, including the desktop toolbar).<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>ppSlideShowDone<\/B>. Setting this value to 5 causes the slide show to continue until every slide has been shown.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>After defining the three constants we create an instance of the <B>PowerPoint.Application<\/B> object and then set the <B>Visible<\/B> property to True; we then use the <B>Open<\/B> method to open the file C:\\Scripts\\Test.ppt. With the presentation open we set several properties of the slide show, including <B>AdvanceOnTime<\/B> and <B>AdvanceMode <\/B>(which, taken together, cause the presentation to run automatically), and <B>ShowType<\/B>, 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.<\/P>\n<TABLE id=\"EZE\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P><B>Note<\/B>. So what if you don\u2019t want to use preconfigured slide timings? No problem; just add this property:<\/P><PRE class=\"codeSample\">objPresentation.Slides.Range.SlideShowTransition.AdvanceTime = 2\n<\/PRE>\n<P>The <B>AdvanceTime<\/B> 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.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>That leaves us with just two properties \u2013 <B>StartingSlide<\/B> and <B>EndingSlide<\/B> \u2013 that we need to configure. (OK, these are actually optional, but we tossed them in because they\u2019re 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 (<B>objPresentation.Slides.Count<\/B>):<\/P><PRE class=\"codeSample\">objPresentation.SlideShowSettings.StartingSlide = 1\nobjPresentation.SlideShowSettings.EndingSlide = objPresentation.Slides.Count\n<\/PRE>\n<P>What if we wanted to show just a portion of the show, say, slides 18-31? All you had to do was ask:<\/P><PRE class=\"codeSample\">objPresentation.SlideShowSettings.RangeType = 2\nobjPresentation.SlideShowSettings.StartingSlide = 18\nobjPresentation.SlideShowSettings.EndingSlide = 31\n<\/PRE>\n<P>As you can see, we simply specified the appropriate starting and ending slides. In addition to that, we also had to set the <B>RangeType<\/B> to 2; that tells PowerPoint that we\u2019re using a specified range of slides as opposed to all the slides in the presentation.<\/P>\n<P>After all the properties are configured we\u2019re ready to run the slide show. To do that we first create an instance of the <B>SlideShowSettings.Run.View<\/B> object. That can be done with a single line of code:<\/P><PRE class=\"codeSample\">Set objSlideShow = objPresentation.SlideShowSettings.Run.View\n<\/PRE>\n<P>As soon as we have the View object we can set up a Do Loop that runs until the slide show has completed:<\/P><PRE class=\"codeSample\">Do Until objSlideShow.State = ppSlideShowDone\n    If Err &lt;&gt; 0 Then\n        Exit Do\n    End If\nLoop\n<\/PRE>\n<P>Don\u2019t 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 \u2013 and visible \u2013 until the entire presentation has been viewed.<\/P>\n<P>Oh, yeah: there <I>is<\/I> an If Then statement in the middle of the loop, isn\u2019t 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\u2019s 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.<\/P>\n<P>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\u2019s no point in staying in the loop if there aren\u2019t 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:<\/P><PRE class=\"codeSample\">objPresentation.Saved = True\nobjPresentation.Close\nobjPPT.Quit\n<\/PRE>\n<P>OK, so what <I>does<\/I> 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 <B>Saved<\/B> property to True; that simply assures PowerPoint that the presentation has been saved (it hasn\u2019t, 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\u2019s over, folks!<\/P>\n<P>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. <\/P>\n<P>Hope that helps, IB. And we hope you\u2019ll agree that the Scripting Guy who writes this column is anything <I>but<\/I> crazy. <\/P>\n<P>Well, OK, sure, he won\u2019t get one of those super-saver cards at the grocery store, even though it would definitely save him money. But that\u2019s about as crazy as he gets. <\/P>\n<P>Other than the fact that, when he eats a sandwich he <I>has<\/I> to eat it from left-to-right; he can\u2019t go from right-to-left or \u2013 heaven forbid! \u2013 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\u2019s watching TV and his team starts to lose he\u2019ll get up and sit in a different chair. And \u2026.<\/P>\n<P>NOTE: The script in this post does not work with PowerPoint 2007. For a script that works with that version see the <A href=\"http:\/\/bit.ly\/3xwSXV\">TechNet Script Center Gallery<\/A>. <\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I run a PowerPoint slide show from a script?&#8212; IB 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 [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[129,49,3,5],"class_list":["post-66543","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-powerpoint","tag-office","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I run a PowerPoint slide show from a script?&#8212; IB 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66543","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=66543"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66543\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=66543"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66543"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66543"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}