3

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)

Community
  • 1
  • 1
user2987808
  • 1,387
  • 1
  • 12
  • 28
  • Try passing an empty array as the second parameter? You might have to do "ModuleName.SubName" as the first param by the way – Jbjstam Jan 09 '17 at 11:23

3 Answers3

0

Try this:

$ppt = New-Object -ComObject powerpoint.application
$presentation = $ppt.presentations.Open("test.pptm")
$ppt.Run("test", @())
sodawillow
  • 12,497
  • 4
  • 34
  • 44
  • Thanks, but that gives: $ppt.Run("test", @()) type must not be ByRef At line:1 char:1 + $ob = $ppt.Run("test", @()) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (:) [], ArgumentException + FullyQualifiedErrorId : System.ArgumentException – user2987808 Jan 09 '17 at 11:26
  • Can you try with `$ppt.Run("test", [ref]@())`? – sodawillow Jan 09 '17 at 11:55
  • ISTR that you can call public subs in add-ins using just the name of the sub (ie, .Run("test") but if you want to call a sub from within a loaded PPT/PPTM file you need to use something like .Run("Test.pptm!test") – Steve Rindsberg Jan 09 '17 at 15:49
  • Both behave the same on my computer. When sub is not found, you get this error: `Application.Run : Invalid request. Sub or function not defined.` – sodawillow Jan 09 '17 at 16:03
0

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

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