在VBA脚本执行后退出CMD窗口

问题描述 投票:0回答:2

我想脚本的执行后退出CMD窗口(手动通过在CMD窗口中书写出目前我退出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
excel vba shell cmd
2个回答
1
投票

你似乎想打开后关闭CMD。 /C标志是这里最好的,因为它执行字符串指定的命令,然后终止。

所以,你应该写:

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

这将正常工作。


0
投票

sCmdLine(其未在您的问题定义),只是在末尾添加&& exit执行所述命令一前一后在单行中。

如果你原来的命令是sCmdLine = "cmd.exe /S /K ping www.google.com",那么这将成为sCmdLine = "cmd.exe /S /K ping www.google.com && exit"

© www.soinside.com 2019 - 2024. All rights reserved.