vba ShellAndWait 不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3919146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ShellAndWait not working
提问by lalachka
I'm using ShellAndWait from here http://www.cpearson.com/excel/ShellAndWait.aspxand I keep getting 1 returned (which means the command didn't work in Windows). But when i paste my cmdLine into Start, Run box it runs fine. Any ideas? I'm using Excel VBA for this and here's my code that I'm calling ShellAndWait with What am I doing wrong?
我从这里使用 ShellAndWait http://www.cpearson.com/excel/ShellAndWait.aspx并且我一直返回 1 (这意味着该命令在 Windows 中不起作用)。但是,当我将 cmdLine 粘贴到“开始”、“运行”框中时,它运行良好。有任何想法吗?我为此使用 Excel VBA,这是我调用 ShellAndWait 的代码,我做错了什么?
thank you very much
非常感谢您
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
回答by HansUp
Enter PAUSEas the last line in Minacs.bat to keep the command window open until you press a key. That should give you a chance to see what's happening.
在 Minacs.bat 中输入PAUSE作为最后一行以保持命令窗口打开,直到您按下某个键。这应该让你有机会看看发生了什么。
You should also revise your VBA code. It calls the same ShellAndWait command at least twice ... once before the If
block, and then again to start the If block. And it can keep trying until it hits an If/ElseIf
condition which matches the return value from ShellAndWait.
您还应该修改您的 VBA 代码。它至少两次调用相同的 ShellAndWait 命令......一次在If
块之前,然后再次启动 If 块。它可以继续尝试,直到遇到If/ElseIf
与 ShellAndWait 的返回值匹配的条件。
Change your code to execute ShellAndWait once time only, and store the return value in a variable. Then you can evaluate the variable in a Select Case
block.
将代码更改为仅执行一次 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
回答by Nick
You can add
你可以加
batchlog.txt
批处理日志.txt
to allow the result that flashes quickly on the screen to be captured to a file.
允许将在屏幕上快速闪烁的结果捕获到文件中。
回答by lalachka
never mind, i just thought to try a different folder, C:\temp and it worked, so it's something with my folder, will keep testing to see what exactly
没关系,我只是想尝试一个不同的文件夹 C:\temp 并且它起作用了,所以它与我的文件夹有关,将继续测试以查看究竟是什么
EDIT
编辑
It's still not working. now it's going through and returns 0 which is Success but the file is not posted to the SFTP site. Here's the exact code i'm using
它仍然无法正常工作。现在它正在通过并返回 0 表示成功,但该文件未发布到 SFTP 站点。这是我正在使用的确切代码
Dim cmdLine As String
cmdLine = "C:\TEMP\Minacs.bat"
cmdLine = "C:\TEMP\Minacs.bat"
ShellAndWait cmdLine, 100000, vbHide, AbandonWait
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