Run PowerPoint Macro from PowerShell

孤街醉人 提交于 2019-12-11 05:16:18

问题


I have a PowerPoint with the following macro:

Sub test()
    MsgBox "testing"
End Sub 

And a PowerShell script like this:

$ppt = New-Object -ComObject PowerPoint.Application
$presentation = $ppt.Presentations.Open("test.pptm")
$ppt.Run("test")

But running the macro just gives:

Cannot find an overload for "Run" and the argument count: "1".
At line:1 char:1
+ $ppt.Run("test")
+ ~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

I get the same error for e.g. $presentation.application.run("test") and $ppt.Run("test.pptm!test").

Related:

Calling Excel macros from PowerShell with arguments

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

The documentation suggests that Run should just take the macro name as a string as its first argument, so I can't see where I'm going wrong.

OverloadDefinitions
-------------------
System.Object Run(string MacroName, [ref] Params System.Object[] safeArrayOfParams)
System.Object _Application.Run(string MacroName, [ref] Params System.Object[] safeArrayOfParams)

Application.Run Method (PowerPoint)


回答1:


Try this:

$ppt = New-Object -ComObject powerpoint.application
$presentation = $ppt.presentations.Open("test.pptm")
$ppt.Run("test", @())


来源:https://stackoverflow.com/questions/41546723/run-powerpoint-macro-from-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!