I have a Powershell script that runs without Error on the Desktop, however when I try to use the PAD action - 'Run a Powershell Script' to run that same script I get the following error message: : An error occurred: You cannot call a method on a null-valued expression.
Any suggestions on how to get this action to run?
This is my code:
# Write the cell value to a text file
$cellValue | Out-File -FilePath "FILEPATH GOES HERE"
try {
# Create an Excel COM object
$excel = New-Object -ComObject Excel.Application
# Open the workbook
$workbook = $excel.Workbooks.Open("FILEPATH GOES HERE.xlsx")
# Check if the workbook is null
if ($workbook -eq $null) {
throw "Failed to open the workbook."
}
# Get the first worksheet
$worksheet = $workbook.Sheets.Item(1)
# Check if the worksheet is null
if ($worksheet -eq $null) {
throw "Failed to get the worksheet."
}
# Get the value of the cell at row 1, column 1
$cellValue = $worksheet.Cells.Item(1, 1).Value()
# Output the cell value
Write-Output "Cell Value: $cellValue"
# Close the workbook without saving
$workbook.Close($false)
}
catch {
Write-Error "An error occurred: $_"
}
finally {
# Quit Excel
if ($excel -ne $null) {
$excel.Quit()
}
# Release COM objects
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($worksheet) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($workbook) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
This is my Error Message:
An error occurred: You cannot call a method on a
null-valued expression.
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,04n2btij40n.tmp.ps1
Exception calling "Quit" with "0" argument(s): "Unable to cast COM object of type
'Microsoft.Office.Interop.Excel.ApplicationClass' to interface type 'Microsoft.Office.Interop.Excel._Application'.
This operation failed because the QueryInterface call on the COM component for the interface with IID
'{000208D5-0000-0000-C000-000000000046}' failed due to the following error: Element not found. (Exception from
HRESULT: 0x8002802B (TYPE_E_ELEMENTNOTFOUND))."
+ $excel.Quit()
+ ~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : InvalidCastException
Exception calling "ReleaseComObject" with "1" argument(s): "Object reference not set to an instance of an object."
+ [System.Runtime.Interopservices.Marshal]::ReleaseComObject($works ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : NullReferenceException
Exception calling "ReleaseComObject" with "1" argument(s): "Object reference not set to an instance of an object."
+ [System.Runtime.Interopservices.Marshal]::ReleaseComObject($workb ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : NullReferenceException