Excel automation fails from PowerShell when PIa are installed.

S

Shiv

Below Script to automate Excel runs fine if PIA are not installed.
$xl=new-object -comObject Excel.Application
$file="D:\test\test.csv"
$xl.visible = $true
$wbk = $xl.Workbooks.Open("$file")
$wks = $wbk.worksheets.item(1)
$r1=$wks.Range("A1:D1")
$r2=$wks.Range("A2:D2")
$xl.Union($r1, $r2)

If PIa are installed it fails on line $xl.Union($r1, $r2). I determined
that it's happening because Windows Powershell is not able to typecast
_ComObject to Microsoft.Office.Interop.Excel.Range. Any pointers?

thanks.
 
Top