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

Trying to group the headline together was quite a challenge ...

I am trying to run several PowerPoint macros from PowerShell. I did a good job running macros from Powershell for Excel. When I run macros in Excel, the Run () method from the COM object will take many arguments, depending on whether the macro has any parameters. However, on the other hand, the PowerPoint Run () method expects parameters, and I cannot decide how to pass them.

My macro expects one line to be passed, I googled plentifully and came up with a short one. I always get this error:

Mistake:

type must not be ByRef 

I put together a very simple PoC for PowerPoint in PowerShell:

The code:

 # PowerPoint test Add-type -AssemblyName office $PowerPoint = New-Object -comobject PowerPoint.Application $PowerPoint.Visible = [Microsoft.Office.Core.MsoTriState]::msoTrue $presentation2 = $PowerPoint.Presentations.open("C:\macros.pptm") $presentation = $PowerPoint.Presentations.open("C:\Test For Macros.pptx") $PowerPoint.run("macros.pptm!IAM",[ref]"Feb") $presentation.save() $presentation.close() $presentation2.close() $PowerPoint.quit() # extra clean up omitted 

The macro itself simply moves the text across all fields; it works great when run from PowerPoint.

Requirements:

Now I want to automate this through several files and slides.

PowerPoint COM object documentation. Run the method , specifying the requirement for two parameters.

+8
vba powershell powerpoint-vba powerpoint
source share
2 answers

Great question and not many examples online, as you say. I was able to delete your example even more and successfully transfer some text to the MsgBox in PowerPoint Macro, without changing what you had.

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

 Sub DisplayMessage(myText As String) MsgBox myText End Sub 

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 mentioned mentions that the module name can be included, so this worked for me too:

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

Maybe try removing [ref] in the line of code below or try changing it to [val]

 $PowerPoint.run("macros.pptm!IAM",[ref]"Feb") 

So either:

 $PowerPoint.run("macros.pptm!IAM","Feb") 

or

 $PowerPoint.run("macros.pptm!IAM",[val]"Feb") 

And then also make sure that the PowerPoint macro expects the variable to be passed to ByVal. For example, something like this:

 Sub IAM(ByVal sMonthName As String) 
+1
source share

All Articles