使用从VBA执行的WinSCP批处理脚本返回FTP上载的状态?

使用从VBA执行的WinSCP批处理脚本返回FTP上载的状态?,vba,batch-file,ftp,winscp,Vba,Batch File,Ftp,Winscp,我正在与多个执行宏的用户共享Excel工作簿,该宏执行以下WinSCP批处理脚本: "C:\Program Files (x86)\WinSCP\WinSCP.com" ^ /command ^ "open ftp://user:pass@ftp.website.org/" ^ "cd /incoming/data" ^ "put ""%~dp0file.txt"&qu

我正在与多个执行宏的用户共享Excel工作簿,该宏执行以下WinSCP批处理脚本:

"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
  /command ^
    "open ftp://user:pass@ftp.website.org/" ^
    "cd /incoming/data" ^
    "put ""%~dp0file.txt""" ^
    "exit"

set WINSCP_RESULT=%ERRORLEVEL%
if %WINSCP_RESULT% equ 0 (
  echo Success
) else (
  echo Error
)

exit /b %WINSCP_RESULT%
脚本从VBA执行,如下所示:

调用Shell(“C:\Users\”环境(“username”)和“\Sharepoint-Library Folder\FTP\ftpupload.bat”)
执行时,命令窗口会出现1-2秒,然后消失。是否有一种方法可以将其保留为成功/错误结果,或者更好的方法是将其传递回VBA,以便我可以在“确定”窗口中显示结果


注意:我希望避免在VBA中注册WinSCP COM,因为此工作簿正由多人使用,我需要尽可能少的先决条件使其保持简单。

您的批处理文件已返回指示错误/成功的退出代码

因此,您只需捕获代码并相应地采取行动。
为此,请参见

Set oSHELL=VBA.CreateObject(“WScript.Shell”)
Dim exitCode为整数
exitCode=oSHELL.Run(““C:\Users\”和环境(“username”)和“\Sharepoint-Library Folder\FTP\ftpupload.bat””,0,True)
如果退出代码为0,则
MsgBox“失败”,vbOKOnly“失败”
其他的
MsgBox“成功”,vbOKOnly“成功”
如果结束

谢谢你,你是个天才,只需要在If:If exitCode 0 then之后加上一个“then”
Set oSHELL = VBA.CreateObject("WScript.Shell")
Dim exitCode As Integer
exitCode = oSHELL.Run("""C:\Users\" & Environ("username") & "\Sharepoint - Library Folder\FTP\ftpupload.bat""", 0, True)
If exitCode <> 0 Then
    MsgBox "Failed", vbOKOnly, "Failure"
Else
    MsgBox "Succeeded", vbOKOnly, "Success"
End If