Passing a variant through COM object via PowerShell to run a macro in PowerPoint

前端 未结 2 1126
萌比男神i
萌比男神i 2021-01-05 01:34

Trying to string the title together was enough of a challenge...

I am trying to run some PowerPoint macros from PowerShell. I have gotten quite good at running macro

2条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-05 01:59

    Great question and not a lot of examples online as you say. I managed to strip your example down even further and successfully pass some text to a MsgBox in a PowerPoint Macro without really changing what you had.

    The Macro in the file PowerShellTest.pptm saved in C:\Temp

    Sub DisplayMessage(myText As String)
      MsgBox myText
    End Sub
    

    The PowerShell script:

    # PowerPoint test
    Add-type -AssemblyName office
    $PowerPoint = New-Object -comobject PowerPoint.Application
    
    $PowerPoint.Visible = [Microsoft.Office.Core.MsoTriState]::msoTrue
    
    $presentation = $PowerPoint.Presentations.open("C:\Temp\PowerShellTest.pptm")
    
    $PowerPoint.run("PowerShellTest.pptm!DisplayMessage",[ref]"Feb")
    

    The Run method documentation link you provided mentions that the module name may be included so this worked for me too:

    $PowerPoint.run("PowerShellTest.pptm!Module1.DisplayMessage",[ref]"Feb")
    

提交回复
热议问题