How Can I Apply a New Template to a PowerPoint Presentation?


Hey, Scripting Guy! Question

Hey, Scripting Guy! My company has just created a new PowerPoint template, and has decided that we need to use this template for all our presentations. How can I apply this new template to all the presentations in my C:\Presentations folder?

— OI

SpacerHey, Scripting Guy! AnswerScript Center

Hey, OI. You know, this morning the Scripting Guy who writes this column did what he does pretty much every weekday morning: stood around the kitchen and waited for the Starbucks iCup coffee maker to brew him a “bean-to-cup, on-demand” cup of coffee.

Note. In case you’re not familiar with the machine, iCup is short for “Interactive Cup Brewer,” apparently to distinguish it from non-interactive coffee makers. (No one likes those.) Interestingly enough, when the Scripting Guy who writes this column was in elementary school, the term iCup was the basis for one of those hilarious-at-the-time grade school jokes: “Spell iCup.” (Spell it and see for yourself.)

You know, come to think of it, that’s still pretty funny, isn’t it?

At any rate, as the Scripting Guy who writes this column waited for the iCup to interactively brew him a cup of coffee, he spent some time perusing the notices tacked up on the bulletin board. The one that caught his attention was an ad for a local dog boarding facility; what he found interesting is that the facility charges by the pound: it’s $20 a day for dogs who weigh between 30 and 49 pounds; $25 a day for dogs who weigh between 50 and 79 pounds; and so on. That’s fine, except that the Scripting Guy who writes this column was struck by a terrifying thought: what if hotels started charging their customers by the pound? Considering the fact that he and fellow Scripting Guy Jean Ross are headed for Barcelona in November, and considering the fact that he’ll need to stay in a hotel on that trip, well …. We don’t need to tell you that he immediately set down that second doughnut.

OK, granted, he picked it right back up again. But at least the notion gave him pause for thought.

Note. In case you’re wondering, back in the old days Microsoft employees who wanted a cup of coffee walked into the kitchen, set their cup under the spigot for the coffee urn, and pulled the handle; two seconds later they had a cup of coffee. Now you walk into the kitchen, set your cup on the iCup, and punch the buttons; 60 seconds later you have a cup of coffee.

Well, unless you’re like the typical Microsoft employee, who removes the cup after 55 seconds. And yes, that does cause coffee to splatter all over the counter. Unfortunately, no one seems to have picked up on that.

Inspired by the dog boarding place, the Scripting Guy who writes this column toyed with the idea of charging by the pound for today’s script, a script that can apply a new template to all the PowerPoint presentations in a folder. However, today’s script is practically weightless: as it turns out, this is a task you can accomplish with just a few lines of code. Therefore, today’s script is free. As for tomorrow’s, well, we’ll see.

Note. Never mind. The Scripting Editor just told us, “No, we won’t see. Tomorrow’s column, and all our columns, will be free.”

And yes, she’s always like that. Probably drinks too much coffee.

Editor’s Note: Maybe, but at least she doesn’t have to worry about a per-pound charge at hotels. Even after eating a doughnut. Or two.

Let’s start out by showing you a script that applies a new template to a single PowerPoint presentation. We’ll explain how that scripts works, then show you a modified version that can apply a template to all the presentations in a folder. Here’s the first script:

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

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

objPresentation.ApplyTemplate _ (“C:\Program Files\Microsoft Office\Templates\Presentation Designs\Ocean.pot”)



As you can see, we start out by creating an instance of the PowerPoint.Application object. We then set the Visible property to True, enabling us to see this instance of PowerPoint on screen. Admittedly, you might think that it’s silly to set the Visible property to True; after all, if everything goes according to plan PowerPoint is only going to be onscreen for a split second. (It will pop up, the template will be changed, and then we’ll save the file and close the application.) For some reason, however, PowerPoint wants to be visible when we go to change the template; if it’s not, the script will crash with the following error:

C:\Scripts\test.vbs(4, 1) Microsoft Office PowerPoint 2003: Presentations.Open : 
Invalid request.  The PowerPoint Frame window does not exist.

Fortunately, setting the Visible property to True takes care of that problem.

As soon as PowerPoint is up and running we call the Open method to open the file C:\Presentations\Test.ppt:

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

In case you’re dying to know, the first slide in this presentation looks like this:


Of course, we don’t want the slide to look that; we want to apply a different template to the presentation. That’s what this line of code is for:

objPresentation.ApplyTemplate _
    (“C:\Program Files\Microsoft Office\Templates\Presentation Designs\Ocean.pot”)

As you can see, all we have to do is call the ApplyTemplate method, passing the complete file path to the template file as the sole method parameter. Now take a look at slide 1, after ApplyTemplate has had a chance to work its magic:


Not bad, eh?

After that we call the Save method to save the modified presentation, then call the Quit method to dismiss our instance of PowerPoint. At that point, our work is done.

Well, unless our work involves applying this same template to all the presentations in a folder. Without going into any detail, here’s a script that does just that, applying the Ocean.pot template to all the PPT files in the folder C:\Presentations:

strComputer = “.”

Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)

Set colFileList = objWMIService.ExecQuery _ (“ASSOCIATORS OF {Win32_Directory.Name=’C:\Presentations’} Where ” _ & “ResultClass = CIM_DataFile”)

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

For Each objFile In colFileList If objFile.Extension = “ppt” Then Set objPresentation = objPPT.Presentations.Open(objFile.Name) objPresentation.ApplyTemplate _ (“C:\Program Files\Microsoft Office\Templates\Presentation Designs\Ocean.pot”) objPresentation.Save objPresentation.Close End If Next


And now our work is done.

Incidentally, if any of you know of a good dog boarding kennel in Barcelona, well, please drop us a line. Admittedly, it’s more traditional to stay in a hotel when attending a trade show. But take a look at the description for Mazzu’s Canine & Feline Luxury Hotel in Philadelphia:

Personal suite, platform bed, comforter, toys, TV/DVD, two walks, one 40-minute jaunt to the dog park, feedings, unlimited bottled water, climate-controlled facility, daily maid service, 24-hour on-site care.

And, for just $25 more, they’ll toss in a filet mignon dinner delivered to your room. That’s way better service than what you get at a people hotel.

Or at least at the people hotels that Microsoft is willing to pay for.