'Calling Excel Macro with parameters from PowerShell
I'm very new to PowerShell scripting and Excel Macros. I have a requirement of calling a Macro that applies some formatting template on an excel and saves the resultset as a new xls file in the path provided as an argument to the macro. I have embedded the macro within example.xlsm file and trying to execute the macro embedded in example.xlsm from a powershell script. I am sure that there are no issues with the excel macro. Upon executing the excel macro, I'm getting an error stated below. Please help.
Error:
You cannot call a method on a null-valued expression.
At C:\Users\usha\Desktop\psexample.ps1:14 char:1
$app.Run("'example.xlsm'!Module1.X2R", "C:\DestinationPath\123.xls")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Powershell script :
$excel = New-Object -comobject Excel.Application
$FilePath = 'C:\Users\usha\Desktop\example.xlsm' #<------- Change this!!!
$workbook = $excel.Workbooks.Open($FilePath)
$app.Run("'example.xlsm'!Module1.X2R", "C:\DestinationPath\123.xls")
$excel.Quit()
exit
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
