PowerShell脚本 – Excel工作簿未关闭

huangapple go评论67阅读模式
英文:

PowerShell script - Excel workbook not closing

问题

我已经使用VBScript +任务计划程序来自动化Excel VBA的启动,它运行得很好,除了一些与随机PowerQuery问题相关的烦恼,会导致脚本中断。现在我正在尝试从VBScript过渡到更现代的PowerShell,并运行下面的脚本。

它可以打开工作簿并成功运行VBA代码,但我无法关闭工作簿。我在这里缺少什么?

$excel = New-Object -comobject Excel.Application  

$FilePath = 'mypath\myfile.xlsm'
$workbook = $excel.Workbooks.Open($FilePath)  

$excel.Visible = $true  

$excel.Run("Pad_check")

$workbook.Close()
$excel.Quit()

Remove-Variable excel
exit

与PowerShell相比,基于VBScript的Excel自动化指令要多得多。

这段代码似乎在 $workbook.Close() 处卡住,甚至没有产生错误。如果我删除这行,Excel会要求确认是否保存工作簿($excel.Quit() 似乎可以正常工作),但这对自动化流程来说不可行。

如何正确地首先关闭工作簿,然后关闭Excel应用程序(这在VBScript中是常见做法)?我可能可以通过在VBA中使用 ThisWorkBook.Saved = True 语句来克服这个问题,而不需要单独关闭工作簿,但我想要了解如何在PowerShell中优雅地实现这一点。

英文:

I have used VBScript + task scheduler to automate Excel VBA launches and it works good, aside of some annoyances related to random PowerQuery issues stopping the script flow. Testing the transition from VBScript to more modern PowerShell and running the script below.

It opens the workbook and runs VBA code fine but I'm not able to close the workbook. What am I missing here?

$excel = New-Object -comobject Excel.Application  

$FilePath = 'mypath\myfile.xlsm'
$workbook = $excel.Workbooks.Open($FilePath)  

$excel.Visible = $true  

$excel.Run("Pad_check")

$workbook.Close()
$excel.Quit()

Remove-Variable excel
exit

Much more VBScript-based Excel automation instructions available compared to PowerShell.

This code seems to hang on $workbook.Close() and not even produce an error. If I remove this line, Excel asks for workbook saving confirmation ($excel.Quit() seems to work) - this is a no-go for automated flow anyway.

How to properly close workbook first and then Excel application (as is common practice with VBScript)? I could possibly overcome this with ThisWorkBook.Saved = True statement in VBA and not need to close the workbook separately, but trying to understand how to do this elegantly in PowerShell.

答案1

得分: 0

你提到你使用了Power Query。如果Power Query在后台运行,那么在查询完成之前,控制可能已经返回到宏,然后返回到脚本。在Power Query刷新时,你不能关闭工作簿。

解决方法是确保Power Query未配置为在后台运行。你可以使用“数据”,“查询和连接”,然后逐个选择所有Power Query的属性,并取消选中“后台查询”,或者你可以使用以下代码 - 调用SetAllQueriesBackground False

Sub SetAllQueriesBackground(Optional setOn As Boolean = False)
'Sets the BackgroundQuery setting for all queries in the workbook
'setOn = True sets BackgroundQuery = True
'As default PQ behaviour is to set this on, default behaviour for this function is to turn it off

    Dim objConnection As Object
    For Each objConnection In ThisWorkbook.Connections
        objConnection.OLEDBConnection.BackgroundQuery = setOn
    Next

End Sub
英文:

You've mentioned that you have used Power Query. If the Power Queries are running in the background then control will probably have returned to the macro and thence to the script, before the queries have completed. You cannot close a workbook while a Power Query is refreshing.
The solution is to ensure that the power queries are not configured to run in the background. You can use Data, Queries & Connections, then select the Properties of all of your Power Queries one by one and untick the Background Query or you can use the following code - call SetAllQueriesBackground False

Sub SetAllQueriesBackground(Optional setOn As Boolean = False)
'Sets the BackgroundQuery setting for all queries in the workbook
'setOn = True sets BackgroundQuery = True
'As default PQ behaviour is to set this on, default behaviour for this function is to turn it off

    Dim objConnection As Object
    For Each objConnection In ThisWorkbook.Connections
        objConnection.OLEDBConnection.BackgroundQuery = setOn
    Next

End Sub

答案2

得分: 0

添加 false 参数到 $workbook.Close($false) 解决了问题。谢谢 @Super Symmetry - 抱歉,我不认为我可以将评论标记为解决方案。

英文:

Adding false-parameter to $workbook.Close($false) solved the issue. Thank you @Super Symmetry - sorry, don't think I can mark comment as solution.

huangapple
  • 本文由 发表于 2023年7月20日 18:38:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729043.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定