Error while Writing Powershell to Open Powerpoint, Run Macros, and Close.

Anthony Partridge 21 Reputation points
2021-03-06T19:23:12.34+00:00

Using Powerpoint 2016

Windows 10

Running Macro in Power point. Setting it to run with a PS1 file, but getting error on application.run cmd.

Error is "Cannot find an overload for "Run" and the argument count: "1".
At line:9 char:1"

I am setting this up to create a bat file and task scheduler. The file opens but then I get an error upon running the script. Below is my script. Using Office 2016 professional. I have tried both just using the Macroname "mailme" and "Module1.mailme" Any help?

PS1 Script is:

# start powerpoint 
$ppt = New-Object -ComObject PowerPoint.Application 

#open file 
$FilePath = 'C:\Users\apartridge\Desktop\Powershell Script\Alerts.pptm' 
$presentation = $ppt.Presentations.Open($FilePath)  

#access the Application object and run a macro  
$ppt.Run("Module1.mailme") 
$ppt.Quit() 

Macro is (running Macro does exactly what it is supposed to)

Sub mailme()

Dim outlookApp As Object

Dim outlookMail As Object

Dim currPres As Presentation

Dim singleSlide As Presentation

Dim L As Long

Set outlookApp = CreateObject("Outlook.Application")

Set outlookMail = outlookApp.CreateItem(0)

Set currPres = ActivePresentation

currPres.SaveCopyAs Environ("TEMP") & "\Current_alerts.pptx"

Set singleSlide = Presentations.Open(Environ("TEMP") & "\Current_alerts.pptx")

For L = singleSlide.Slides.Count To 1 Step -1

    If L <> 1 Then  ' three would be the slide to keep alter as needed

        singleSlide.Slides(L).Delete

    End If

Next L

singleSlide.Save

On Error Resume Next

With outlookMail

    .To = "******@company.com" ' change of course

    .Subject = "Alerts"

    .body = "Chart shows current and previous month alerts" & vbCrLf & "Alerts can be found in L:\QMS\Records\Quality Alert\2021"

    .Attachments.Add singleSlide.FullName

    .Send

End With

singleSlide.Close


Kill (Environ("TEMP") & "\Current_alerts.pptx")


End Sub
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
4,366 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,628 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 47,896 Reputation points
    2021-03-08T03:40:53.457+00:00

    With absolutely no guaranty, try this:

    Function Invoke-NamedParameter {
        [CmdletBinding(DefaultParameterSetName = "Named")]
        param(
            [Parameter(ParameterSetName = "Named", Position = 0, Mandatory = $true)]
            [Parameter(ParameterSetName = "Positional", Position = 0, Mandatory = $true)]
            [ValidateNotNull()]
            [System.Object]$Object
            ,
            [Parameter(ParameterSetName = "Named", Position = 1, Mandatory = $true)]
            [Parameter(ParameterSetName = "Positional", Position = 1, Mandatory = $true)]
            [ValidateNotNullOrEmpty()]
            [String]$Method
            ,
            [Parameter(ParameterSetName = "Named", Position = 2, Mandatory = $true)]
            [ValidateNotNull()]
            [Hashtable]$Parameter
            ,
            [Parameter(ParameterSetName = "Positional")]
            [Object[]]$Argument
        )
    
        end {  ## Just being explicit that this does not support pipelines
            if ($PSCmdlet.ParameterSetName -eq "Named") {
                ## Invoke method with parameter names
                ## Note: It is ok to use a hashtable here because the keys (parameter names) and values (args)
                ## will be output in the same order.  We don't need to worry about the order so long as
                ## all parameters have names
                $Object.GetType().InvokeMember($Method, [System.Reflection.BindingFlags]::InvokeMethod,
                    $null,  ## Binder
                    $Object,  ## Target
                    ([Object[]]($Parameter.Values)),  ## Args
                    $null,  ## Modifiers
                    $null,  ## Culture
                    ([String[]]($Parameter.Keys))  ## NamedParameters
                )
            } else {
                ## Invoke method without parameter names
                $Object.GetType().InvokeMember($Method, [System.Reflection.BindingFlags]::InvokeMethod,
                    $null,  ## Binder
                    $Object,  ## Target
                    $Argument,  ## Args
                    $null,  ## Modifiers
                    $null,  ## Culture
                    $null  ## NamedParameters
                )
            }
        }
    }
    
    
    
    
    $ppt = New-Object -ComObject PowerPoint.Application
    Invoke-NamedParameter $ppt "Run" -argument @("Module.mailme","")
    

    The function was taken from here: how-to-call-a-complex-com-method-from-powershell

    I don't use PowerPoint and have no how to create a macro. I do know that I at least got as far as getting an error telling me "Application.Run : Invalid request. Sub or function not defined.". Whether that means the module.mailme doesn't exist or "Run" doesn't exist I don't know. Give it a try. If it works, that's great. If it doesn't, I got nothing.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Rich Matheisen 47,896 Reputation points
    2021-03-06T20:35:47.84+00:00

    Don't you have to pass a parameter list, too? The error says that there no match for the Run command with only 1 parameter.

    Try: $ppt.Run("Module1.mailme", @()) or maybe $ppt.Run("Module1.mailme", "")

    There's a specific type that can be passed the says a parameter is missing, but I don't know how to represent that in PowerShell. I think it's System.Reflector.Type.Missing.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.