2
votes

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)

3
Try passing an empty array as the second parameter? You might have to do "ModuleName.SubName" as the first param by the wayJbjstam

3 Answers

0
votes

Try this:

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

Based on this post:

$ppt = New-Object -ComObject PowerPoint.Application
$presentation = $ppt.Presentations.Open("test.pptm")
$presname = $presentation.Name
$VBScript = New-Object -ComObject "MSScriptControl.ScriptControl"
$VBscript.Language = "VBScript"
$VBscript.AddCode( 
@"
  Function RunVBA( app, functionName ) 
    RunVBA = app.Run( functionName, array() )
  End Function
"@
)

$VBscript.CodeObject.RunVBA( $ppt, "$presname!test" )

Note that this only works with Powershell x86, since ScriptControl is not available in a 64-bit environment.

0
votes

Accessing the Application object via the presentation works for me:

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