I am running below code -



[Microsoft.Office.Interop.Excel.Application]$xl = new-object


$xl.visible = $true

$wbk = $xl.Workbooks.Open("$file")

$wks = $wbk.worksheets.item(1)


#Commented code


#$mis = [System.Reflection.Missing]::Value;

#$xl.Union($r5, $r6, $mis, $mis, $mis, $mis, $mis, $mis, $mis, $mis, $mis,
$mis, $mis, $mis, $mis, $mis, $mis, $mis, $mis, $mis, $mis, $mis, $mis, $mis,
$mis, $mis, $mis, $mis, $mis, $mis)


It throws below error while I try to typecast _ComObject returned to IRange
(To run the script, just create a new Excel file and save it to .csv format

Error message -

$r1=[Microsoft.Office.Interop.Excel.IRange]$wks.Range <<<< ("A11")

+ CategoryInfo : NotSpecified: ( [], RuntimeException

+ FullyQualifiedErrorId : RuntimeException

Typecasting is required otherwise the next command to union two range
(commented code) fails with same error.

I am stuck on it. Please advice.