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)

2
  • Try passing an empty array as the second parameter? You might have to do "ModuleName.SubName" as the first param by the way Commented Jan 9, 2017 at 11:23
  • I've resolved my issue using Invoke-NamedParameter from stackoverflow.com/questions/5544844/… Commented Sep 18, 2023 at 14:45

3 Answers 3

0

Try this:

$ppt = New-Object -ComObject powerpoint.application $presentation = $ppt.presentations.Open("test.pptm") $ppt.Run("test", @()) 
Sign up to request clarification or add additional context in comments.

4 Comments

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
Can you try with $ppt.Run("test", [ref]@())?
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")
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.
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.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.