我正在使用以下代码通过VBA运行Unix命令。我想实现两件事:
我的问题是每次程序运行完毕并且我手动关闭cmd窗口时,VBA会给我一个“溢出”错误。错误发生在
errorcode = wsh.Run("%comspec% /k" & cmd, 1, True)
我可以改变这一行
errorcode = wsh.Run("%comspec% /c" & cmd, 1, True)
但随后窗口闪烁,我无法阅读消息。我该如何解决?提前谢谢!
Sub RunTemplate_Wait()
Dim cmd As String
Dim wsh As Object
Dim pCommand As String
Dim errorcode As Integer
Application.StatusBar = "Running " & ProgramName & "..."
pCommand = "cd " & ServerPath & "; nohup ksh autowoe.ksh"
cmd = Chr(34) & "C:\Program Files (x86)" & "\PuTTY\plink.exe" & Chr(34) & " -l " & pUser & " -pw " & pPass & " -P 22 -2 -ssh " & pHost & " " & pCommand
Debug.Print cmd
Set wsh = CreateObject("wscript.shell")
errorcode = wsh.Run("%comspec% /k" & cmd, 1, True)
If errorcode = 0 Then
MsgBox (ProgramName & " runs successfully.")
Application.StatusBar = "Complete " & ProgramName & "."
Else
MsgBox (ProgramName & " fails to run. Error Occurs.")
Application.StatusBar = "Failed to run " & ProgramName & "."
End If
End Sub