我在这里使用ShellAndWait http://www.cpearson.com/excel/ShellAndWait.aspx并且我一直返回1(这意味着该命令在Windows中不起作用)。但是当我将cmdLine粘贴到“开始”,“运行”框中时,它运行正常。有任何想法吗?我正在使用Excel VBA,这是我的代码,我正在调用ShellAndWait 我做错了什么?
非常感谢
Sub test()
Dim cmdLine As String
cmdLine = "C:\Documents and Settings\natalie.rynda\My Documents\Marta\Calling Files\_SFTP\Minacs.bat"
ShellAndWait cmdLine, 1000, vbHide, PromptUser
If ShellAndWait(cmdLine, 1000, vbHide, PromptUser) = 0 Then
MsgBox "yes!!!!!!"
ElseIf ShellAndWait(cmdLine, 1000, vbHide, PromptUser) = 1 Then
MsgBox "1"
ElseIf ShellAndWait(cmdLine, 1000, vbHide, PromptUser) = 2 Then
MsgBox "2"
ElseIf ShellAndWait(cmdLine, 1000, vbHide, PromptUser) = 3 Then
MsgBox "3"
ElseIf ShellAndWait(cmdLine, 1000, vbHide, PromptUser) = 4 Then
MsgBox "4"
ElseIf ShellAndWait(cmdLine, 1000, vbHide, PromptUser) = 5 Then
MsgBox "5"
ElseIf ShellAndWait(cmdLine, 1000, vbHide, PromptUser) = 6 Then
MsgBox "6"
End If
End Sub
答案 0 :(得分:2)
输入 PAUSE 作为Minacs.bat中的最后一行,以便在按下某个键之前保持命令窗口打开。这应该让你有机会看到发生了什么。
您还应该修改您的VBA代码。它至少调用两次相同的ShellAndWait命令...一次在If
块之前,然后再次启动If块。并且它可以继续尝试,直到它达到If/ElseIf
条件,该条件与ShellAndWait的返回值匹配。
更改代码以仅执行一次ShellAndWait,并将返回值存储在变量中。然后,您可以在Select Case
块中评估变量。
Sub test()
Dim cmdLine As String
Dim strMsg As String
Dim lngResult As Long
cmdLine = "C:\TEMP\Minacs.bat"
'lngResult = ShellAndWait(cmdLine, 100000, vbHide, AbandonWait) '
lngResult = ShellAndWait(cmdLine, 100000, vbNormalFocus, AbandonWait)
Select Case lngResult
Case 0
'does some stuff here, like send an email, omitted '
Case 1
strMsg = "The file hasn't been uploaded." & vbCrLf & _
"Wait operation failed due to a Windows error."
Case 2
strMsg = "The file hasn't been uploaded." & vbCrLf & _
"The operation timed out."
Case 3
strMsg = "The file hasn't been uploaded." & vbCrLf & _
"An invalid value was passed to the procedure."
Case 4
strMsg = "The file hasn't been uploaded." & vbCrLf & _
"The system abandoned the wait."
Case 5, 6
strMsg = "The file hasn't been uploaded." & vbCrLf & _
"The user abandoned the wait."
Case Else
strMsg = "WTF?!!!"
End Select
If Len(strMsg) > 0 Then
MsgBox strMsg
End If
End Sub
答案 1 :(得分:1)
您可以添加
batchlog.txt
允许将屏幕上快速闪烁的结果捕获到文件中。
答案 2 :(得分:0)
没关系,我只是想尝试一个不同的文件夹,C:\ temp并且它有效,所以这是我的文件夹,将继续测试,看看到底是什么
修改强>
它仍然无效。现在它正在通过并返回0,这是成功但文件未发布到SFTP站点。这是我正在使用的确切代码
Dim cmdLine As String
cmdLine =“C:\ TEMP \ Minacs.bat”
ShellAndWait cmdLine,100000,vbHide,AbandonWait
If ShellAndWait(cmdLine, 100000, vbHide, AbandonWait) = 0 Then
'does some stuff here, like send an email, omitted
ElseIf ShellAndWait(cmdLine, 100000, vbHide, AbandonWait) = 1 Then
MsgBox "The file hasn't been uploaded." & vbCrLf & "Wait operation failed due to a Windows error."
ElseIf ShellAndWait(cmdLine, 100000, vbHide, AbandonWait) = 2 Then
MsgBox "The file hasn't been uploaded." & vbCrLf & "The operation timed out."
ElseIf ShellAndWait(cmdLine, 100000, vbHide, AbandonWait) = 3 Then
MsgBox "The file hasn't been uploaded." & vbCrLf & "An invalid value was passed to the procedure."
ElseIf ShellAndWait(cmdLine, 100000, vbHide, AbandonWait) = 4 Then
MsgBox "The file hasn't been uploaded." & vbCrLf & "The system abandoned the wait."
ElseIf ShellAndWait(cmdLine, 100000, vbHide, AbandonWait) = 5 Or ShellAndWait(cmdLine, 10000, vbHide, AbandonWait) = 6 Then
MsgBox "The file hasn't been uploaded." & vbCrLf & "The user abandoned the wait."
End If