使用VBA运行命令行 - 溢出错误

时间:2016-11-22 13:25:59

标签: excel vba excel-vba unix

我正在使用以下代码通过VBA运行Unix命令。我想实现两件事:

  1. 保持cmd窗口打开,以便用户可以更好地查看进度
  2. 有一个“errorcode”,如果程序成功运行则返回0,如果是,则返回1 该计划未能运行。
  3. 我的问题是每次程序运行完毕并且我手动关闭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  
    

0 个答案:

没有答案