gpt4 book ai didi

excel - 在 VBA 中执行脚本后退出 Cmd 窗口

转载 作者:行者123 更新时间:2023-12-04 22:29:19 32 4
gpt4 key购买 nike

我想在执行脚本后退出 CMD 窗口(目前我通过在 CMD 窗口中写入 exit 手动退出 CMD,之后 VBA 继续运行)

Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 1
Dim errorCode As Long
errorCode = wsh.Run(sCmdline, windowStyle, waitOnReturn)

'-- Finally Run function Arguments -----
errorCode = wsh.Run("cmd /k systeminfo >D:\Tools\MyScript\SystemInfo.txt", windowStyle, waitOnReturn)

If errorCode = 0 Then
MsgBox "Done! No error to report."
Set wsh = Nothing
Else
MsgBox "Program exited with error code " & errorCode & "."
Set wsh = Nothing
End If

最佳答案

您似乎想在打开 cmd 后关闭它。 /C flag 在这里最好,因为它执行 string 指定的命令然后终止。

所以,你应该写:

errorCode = wsh.Run("cmd /c systeminfo >D:\Tools\MyScript\SystemInfo.txt", windowStyle, waitOnReturn)

这将起作用。

关于excel - 在 VBA 中执行脚本后退出 Cmd 窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54567974/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com