Cannot Call A Method On A Null (Excel WorkSheet)

2019-07-22 21:07发布

I'm currently trying to grab a specific worksheet from an Excel file in PowerShell and I'm receiving the error "You cannot call a method on a null-valued expression". PowerShell picks up the workbook and even that the property Worksheet exists, yet it does not recognize item(1).

$excel = "C:\budget.xlsx"
$ex = New-Object -ComObject Excel.Application
$ex.Visible = $true
$wb = $ex.Workbooks.Open($excel)
$ws = $wb.Worksheets.item(1) # This line errors

When accessing Excel worksheets, this is the syntax (for instance, see this code, and this is just one of many examples). Is there something else that needs to be loaded in order to do this? After searching the 'net, I can't find anyone else loading or using another syntax.

0条回答
登录 后发表回答