May 13th, 2010

Hey, Scripting Guy! How Can I Customize Microsoft PowerPoint Presentations?

Bookmark and Share

 

Hey, Scripting Guy! Question

 Hey, Scripting Guy! I am a sales person, and therefore not a professional IT person. I live and die on my ability to use Microsoft PowerPoint. I therefore stumbled across your Web site while searching for solutions to a problem I have. Let me briefly explain the situation. I learned a long time ago that customers love it when I personalize my slide deck for them. It shows that I care, and in addition, they perceive the presentation as tailored to their needs. The problem is that I may visit as many as 20 different customers a week, and therefore that requires a lot of time spent adding slides, saving the presentation with new names, and trying to remember to change the name of the last customer before my next presentation. As you can see, it is quite a dilemma. I was therefore intrigued when I ran across your site, so I am wondering if you could see your way clear to assist me. You would be helping the global economy to grow by increasing my productivity. What do you say, pal? If you will not help me, would you be willing to help the world?

— AN

 

Hey, Scripting Guy! AnswerHello AN,

Microsoft Scripting Guy Ed Wilson here. I am glad you decided to be brief – I would have hated to sit through your long version. Anyway, I completely understand your situation. Back when I was a Microsoft Technical Account Manager (TAM), I used to personalize the presentations for my customers. However, I only had a few customers, so it made it really easy. Windows PowerShell was not invented back then either. Your suggestion was pretty cool and I decided to write the ReadCSVandAddPowerPointSlide.ps1 script for you. The complete ReadCSVandAddPowerPointSlide.ps1 script is seen here.

ReadCSVandAddPowerPointSlide.ps1

Add-type -AssemblyName office
$Application = New-Object -ComObject powerpoint.application
$application.visible = [Microsoft.Office.Core.MsoTriState]::msoTrue
$slideType = “microsoft.office.interop.powerpoint.ppSlideLayout” -as [type]
$templatePresentation = “C:fsoTemplatePresentation.pptx”
Import-Csv -Path C:fsopptTemplateNames.csv | ForEach-Object { `
 $presentation = $application.Presentations.open($templatePresentation)
 $customLayout = $presentation.Slides.item(2).customLayout
 $slide = $presentation.slides.addSlide(1,$customLayout)
 $slide.layout = $slideType::ppLayoutTitle
 $slide.Shapes.title.TextFrame.TextRange.Text = $_.group
 $slide.shapes.item(2).TextFrame.TextRange.Text = $_.date

 $presentation.SavecopyAs(“C:fso$($_.group)”)
 $presentation.Close()
 “Created $($_.group)”
}

$application.quit()
$application = $null
[gc]::collect()
[gc]::WaitForPendingFinalizers()

The first thing that must be done is to add the office assembly to the current Windows PowerShell session, create the PowerPoint application object, set it visible, and create the b type. This was discussed in Monday’s Hey, Scripting Guy! post.

Add-type -AssemblyName office
$Application = New-Object -ComObject powerpoint.application
$application.visible = [Microsoft.Office.Core.MsoTriState]::msoTrue
$slideType = “microsoft.office.interop.powerpoint.ppSlideLayout” -as [type]

After that has been accomplished, the path to the template presentation is assigned to the $templatePresentation variable. The template presentation will form the basis for the new Microsoft presentations that will be created. As seen in the following image, the presentation contains several slides and has a specific theme applied to it.

Image of presentation template

 

The code that assigns the path to the template presentation is shown here:

$templatePresentation = “C:fsoTemplatePresentation.pptx”

The new slide that will be inserted into each new presentation will be customized with the date of the presentation and the name of the specific group that will receive the presentation. Rather than using a plain text file to hold this information, a comma-separated value (CSV) file is used. As seen in the following image, CSV files can be created, displayed, and edited in Microsoft Excel.

Image of CSV file being viewed in Microsoft Excel

 

The good thing about a CSV file, however, is it is easily consumed in Windows PowerShell by using the Import-CSV cmdlet. The first line of the CSV file contains the field names. These names are used to refer to the data that is stored in each column of the CSV file. The results of importing the CSV are piped to the ForEach-Object cmdlet that allows us to work on each line as it comes across the pipeline. This section of the script is shown here:

Import-Csv -Path C:fsopptTemplateNames.csv | ForEach-Object { `

The template presentation is opened by using the Open method from the Presentation object. The returned Presentation object is stored in the $presentation variable. Next a CustomLayout object is retrieved from the second slide in the template presentation, and the object is stored in the $customLayout variable. The AddSlide method from the Slides collection object is used to add a slide with the layout stored in the $customLayout variable in the first position. These lines of code were all discussed in yesterday’s Hey, Scripting Guy! post. This section of the script is shown here:

$presentation = $application.Presentations.open($templatePresentation)
 $customLayout = $presentation.Slides.item(2).customLayout
 $slide = $presentation.slides.addSlide(1,$customLayout)
 $slide.layout = $slideType::ppLayoutTitle

The group property from the CSV file is used to assign the text to the title shape on the slide. The $_ automatic variable is used to refer to the current item on the pipeline. This is shown here:

 $slide.Shapes.title.TextFrame.TextRange.Text = $_.group

The title shape object is the first item in the Shapes collection of the new slide. The second item is the TextFrame under the title. The text property of the TextRange object is used to add the date to the slide. The date is stored in the date property from the CSV file. This section of the script is shown here:

 $slide.shapes.item(2).TextFrame.TextRange.Text = $_.date

The SaveCopyAs method from the presentation object is used to save the newly created Microsoft PowerPoint presentation. The SaveCopyAs method is overloaded and has several optional parameters. Luckily, the first parameter is used to save the file with a new name, and to not make any changes to the current template presentation. The other options are discussed on MSDN.

 $presentation.SavecopyAs(“C:fso$($_.group)”)

One of the newly created Microsoft PowerPoint presentations is shown in the following image.

Image of newly created Microsoft PowerPoint presentation

 

The template presentation is closed after the changes were saved to a new file. In the next loop, the template presentation will be opened and the new slide created for the next group and date on the list. After the presentation has been closed, a message is displayed that states the name of the group that was created. A subexpression is used to prevent unraveling of the group property from the object:

 $presentation.Close()
 “Created $($_.group)”
}

After all of the new presentations have been created, it is time for cleanup. The quit method of the application object is called, the $application variable nulled out, and garbage collection is called. This is shown here:

$application.quit()
$application = $null
[gc]::collect()
[gc]::WaitForPendingFinalizers()

 

AN, that is all there is to using Windows PowerShell to create new Microsoft PowerPoint presentations. This brings Microsoft PowerPoint Week to a close. Join us tomorrow for Quick-Hits Friday.

If you want to know exactly what we will be looking at tomorrow, follow us on Twitter or Facebook. If you have any questions, send e-mail to us at scripter@microsoft.com or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

 

Ed Wilson and Craig Liebendorfer, Scripting Guys

 

Author

1 comment

Discussion is closed. Login to edit/delete existing comments.

  • Raghu Nalamada

    hello, I cannot find any of the images linked in this blog. Please confirm as i’m trying to automate a powerpoint slide from excel as source however after adding of 5 objects it is indicating i’m going outbounds, would like to check the images to give me a better idea what i’m doing.