PowerShell + Excel - $workbook.save() hangs up on

2019-09-04 07:31发布

I started messing around with Excel on PowerShell, specially because I had to go through 138 files changing every instance of $B$1 to TEXT($B$1,"0000") and I didn't want to do that manually.

Found a couple of resources online and changed the formula but when I try .Save(), PowerShell seems to just sit there, waiting for something I have no idea what.

PS C:\> $excel = New-Object -com Excel.Application
PS C:\> Get-ChildItem '\\UNC\Path\to\folder' <Common file string>*.xls -Recurse | Select-Object -First 1 | % {
>> $workbook = $excel.Workbooks.Open($_.Fullname)
>> $workbook.Save()
>> }

Any ideas on what could be the problem?

1条回答
再贱就再见
2楼-- · 2019-09-04 07:51

When Excel hangs in this regard I would suspect it is waiting for you to do something. The easiest was to confirm this is setting the visibility to True.

$excel.Visible = $true

That would just show you the dialog that was hidden from you previously. As discussed in comments it appears you got a message from the Compatibly Checker. Once you know what it is and are willing to suppress it then just have the following

$excel.Visible = $false
$excel.displayAlerts = $false
查看更多
登录 后发表回答