How can I create a new Powerpoint presentation with Powershell

0

I am trying to create a new Powerpoint presentation with Powershell from scratch but am having trouble with the object model. Based on some code from the ScriptingGuy I came up with:

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]

$blanklayout = $slideType::ppLayoutTitleOnly

$presentation = $application.Presentations.add()

$slide = $presentation.slides.addSlide(0,$blanklayout)

but receive an error:

Ausnahme beim Aufrufen von "AddSlide" mit 2 Argument(en):  "Typenkonflikt. (Ausnahme von HRESULT: 0x80020005 
(DISP_E_TYPEMISMATCH))"
In C:\Users\Uwe\Dropbox\Powerpoint.ps1:12 Zeichen:1
+ $slide = $presentation.slides.addSlide(0,$blanklayout)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation

How can I get the correct layout from the object model and add the new slide?

powershell
com
powerpoint
asked on Stack Overflow Nov 21, 2015 by Uwe Ziegenhagen

2 Answers

2

I found referencing this code in the below link helpful.

https://gist.github.com/miriyagi/4240819

Also see my example. Changing the value (15) determines the style of the new slide being inserted.

$slide = $Presentation.Slides.Add($presentation.Slides.Count + 1, 15)
answered on Stack Overflow Nov 21, 2015 by AntoAdam • edited Nov 25, 2015 by AntoAdam
2

The first thing I see is that you need to load the assembly that contains the type microsoft.office.interop.powerpoint.ppSlideLayout. The assembly's name is microsoft.office.interop.powerpoint. So:

add-type -assembly microsoft.office.interop.powerpoint

The second thing I needed to do to get it to work for me was to use the Add method instead of the AddSlide method.

$slide = $presentation.slides.add(1,$blanklayout)

The $blanklayout can be replaced by an integer, if you know which layout the integer corresponds to. However using the code like you have is somewhat more self-documenting. 15 is the value for ppLayoutLargeObject.

The first parameter is an integer between 1 and ($presentation.slides.count + 1). If you choose a value less than the max the slide is inserted into that place in the slide deck and the slides after it have their slide number incremented by 1. If you choose the max the slide is added at the end.

Note that the "Add" method is marked as an internal API in the documentation. Probably not a big deal, but MS would have more justification for changing that API in a future version of Powerpoint than an API not marked internal. Using the AddSlide API looks more complicated. The doc says you have to use a SlideRange to get a CustomLayout.

answered on Stack Overflow Nov 22, 2015 by Χpẘ

User contributions licensed under CC BY-SA 3.0