Search code examples
powershellcompowerpoint

How can I create a new Powerpoint presentation with Powershell


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?


Solution

  • 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)