I have the ff. portion of a bigger Powershell 5.1 script that has been running smoothly for over 2 years:
$Excel = New-Object -Comobject Excel.Application
$Excel.Visible = $True
$Excel.DisplayAlerts = $False
$book = $Excel.Workbooks.Open($OutWDed2H)
$sheet = $book.Worksheets.Item('PPWKS')
$sheet.Unprotect('abc')
When I ran it today (the script has not been changed one bit), it suddenly generated these error messages:
Exception setting "Visible": "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))."
At line:2 char:4
+ $Excel.Visible = $True
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenSetting
Exception setting "DisplayAlerts": "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))."
At line:3 char:4
+ $Excel.DisplayAlerts = $False
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenSetting
You cannot call a method on a null-valued expression.
At line:4 char:4
+ $book = $Excel.Workbooks.Open($OutWDed2H)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At line:5 char:4
+ $sheet = $book.Worksheets.Item('PPWKS')
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At line:6 char:4
+ $sheet.Unprotect('abc')
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Would be grateful for muched needed help, tips and/or guidance. I suspect this has to do with recent Microsoft updates to my .Net or some related Windows OS that impactx EXCEL but have no idea where to begin.
Thanking you in advance.