我使用一个非常简单的 powershell 来打开 Excel 文件、运行宏并保存文件。
该宏基本上是“数据”选项卡下的“刷新全部”按钮。
我的问题是 Excel 卡在“保存”阶段,因为“刷新全部”永远不会完成。
如果我在命令之间添加暂停。我可以毫无问题地正常进步。
clear
$file = "\\website\folder\file.xlsm"
$displayname = $File.split("\")[$File.split("\").count-1]
Write-Host "Creating Excel Instance for $displayname"
$Excel = New-Object -ComObject Excel.Application
Write-Host "Opening $displayname"
$Excel.Visible = $True
$Excel.StatusBar = "Opening the file... Please be patient..."
$Workbook = $Excel.Workbooks.Open($file)
Write-Host "Running Refresh Query"
$Excel.StatusBar = $False
$Excel.Run("Refresh_All")
PAUSE ## This pause is needed in order to wait the Macro to happen
Write-Host "Saving $displayname"
$Workbook.Save()
Write-Host "Close Excel Instance"
$Excel.Quit()
如果尝试读取 $excel.Ready 状态...但它始终准备就绪...
我正在寻找一种方法来读取状态栏并识别正在运行的背景查询消息并在完成时保存文件。
有什么提示吗?
最佳答案
我找到了解决方案...
解决方案:“转到数据选项卡,单击连接组中的连接,单击每个连接,然后单击属性,取消选中“启用后台刷新”框。” ([email protected],pcreview,2013)。
关于excel - Powershell等待Excel中执行宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53834189/