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

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


Great question and not a lot of examples online as you say. I managed to strip your example down even further and successfully pass some text to a MsgBox in a PowerPoint Macro without really changing what you had.

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

Sub DisplayMessage(myText As String)  MsgBox myTextEnd Sub

The PowerShell script:

# PowerPoint testAdd-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 provided mentions that the module name may be included so this worked for me too:

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


Maybe try removing the [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 is expecting the variable to be passed ByVal. For example something like this:

Sub IAM(ByVal sMonthName As String)