I'm new to PowerShell and I need assistance with a script I'm writing to bulk update several Excel workbooks. These workbooks automatically update when they're opened, and I need them to process some calculations as an intermediary step. So far, I've created a new Excel ComObject using `$excel = New-Object -ComObject Excel.Application`. Then, I've listed the workbook paths with `$workbookPaths = @("workbook 1", "workbook 2", ...)`. I'm using a loop to open each workbook as follows:
```
foreach($path in $eorkbookPaths) {
try {
$workbook = $excel.Workbooks.Open($path)
$workbook.Save()
$workbook.Close()
} catch {
Write-Host "Error processing: $path $($_.Exception.Message)"
}
}
$excel.Quit()
```
However, I'm getting the error message: "Error processing: workbook You cannot call a method on a null-valued expression." Any help would be greatly appreciated!
5 Answers
Double-check for typos! It looks like you meant `$workbookPaths` instead of `$eorkbookPaths`. Also, make sure your file paths include the .xlsx extension. If the files aren't in the same folder as your script, you should use absolute paths. Otherwise, PowerShell won’t find them on its own.
Make sure the paths to your workbooks are valid! Throw in some `Write-Host` lines in your loop to track which path is causing issues. That’ll help you identify what's null.
Speculating here, but you might not need `$workbook.Save()` at all. Try changing `$workbook.Close()` to `$workbook.Close($true)` to save and close the workbook. If `$workbook.Save()` is invalid, it could lead to that error you're getting.
I noticed a typo in your script: your loop should say `foreach($path in $workbookPaths)` instead of using `$eorkbookPaths`. That could be the reason for your error.
Have you considered using the PSWriteExcel module? It’s available on PSGallery, and there are plenty of examples on its GitHub page. It might save you some hassle compared to using COM objects.

Related Questions
How To Get Your Domain Unblocked From Facebook
How To Find A String In a Directory of Files Using Linux