Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否可以将错误代码从批处理文件返回到VBA?_Vba_Batch File_Excel - Fatal编程技术网

是否可以将错误代码从批处理文件返回到VBA?

是否可以将错误代码从批处理文件返回到VBA?,vba,batch-file,excel,Vba,Batch File,Excel,我的批处理文件: SET username=%1 SET password=%2 net use "\\gessel\toys\name" %password% /user:shops\%username% ECHO.%ERRORLEVEL% :copy Xcopy "\\gessel\toys\name\babytoys" "%appdata%\shops" /S /E ECHO.%ERRORLEVEL% IF ERRORLEVE

我的批处理文件:

SET username=%1
SET password=%2

net use "\\gessel\toys\name" %password% /user:shops\%username%
ECHO.%ERRORLEVEL%
:copy
Xcopy "\\gessel\toys\name\babytoys" "%appdata%\shops" /S /E
ECHO.%ERRORLEVEL%

IF ERRORLEVEL 0 goto disconnect
goto end
:disconnect 
net use "\\gessel\toys\name\babytoys" /delete
goto end
:end
EXIT /B %ERRORLEVEL%
我从VBA调用了上述批处理文件,代码如下:

call Shell(Environ$("COMSPEC") & " /c " & path & username & password, vbHide)
上面的代码运行良好。但我需要验证文件是否在VBA中复制。假设客户输入了错误的用户名和密码,那么他将无法获得玩具信息。然后,我必须显示一个消息框,将消息显示为“输入的信息错误”。因此,我尝试了如下代码:

sub submit_click

    Dim as error as integer
    error = Shell(Environ$("COMSPEC") & " /c " & path & username & password, vbHide)
    if error <> 0
        MsgBox "Provided info wrong", vbOKOnly, "Failure"
    end if
end sub
子提交\u单击
Dim作为错误作为整数
error=Shell(Environ$(“COMSPEC”)&“/c”&路径、用户名和密码,vbHide)
如果错误为0
MsgBox“提供的信息错误”,vbOKOnly,“失败”
如果结束
端接头

但是上面的代码不起作用。它总是返回值,即使用户名和密码正确。但是,如果我运行批处理文件,它会正确地返回值,例如,对于正确的详细信息0,对于错误的数据是2或4。请任何人帮助我从批处理文件中捕获错误代码并将其传递到VBA。

每个命令执行时
ERRORLEVEL
变量的值都会发生变化(或多或少)。因此,当批处理文件中的代码执行时,每个命令都会生成一个更改。您需要存储该值以供以后处理,或者在您的情况下,在每个步骤中使用相应的值退出:

SET "username=%~1"
SET "password=%~2"

rem This will be tested for a errorlevel value of 1 or greater. In this case, 
rem no further processing will be done if errors found, so we return our own 
rem value to the calling process

net use "\\gessel\toys\name" %password% /user:shops\%username% 
if errorlevel 1 exit /b 100

rem We are connected, and will disconnect no matter if xcopy worked or failed
rem So, the result of the command will be stored to later return the adecuate value

Xcopy "\\gessel\toys\name\babytoys" "%appdata%\shops" /S /E
set "exitCode=%errorlevel%"

net use "\\gessel\toys\name\babytoys" /delete

EXIT /B %exitCode%
现在,在vba代码中,可以测试
error
变量的值100(我们从网络使用中的错误返回的值)或xcopy返回的任何值

现在我们有了一个工作批处理文件,让我们使用Excel

不,VBA中的
Shell
函数不能满足您的要求。
Shell
的返回值是正在运行的进程的id
Shell
不要等待进程结束,因此它无法返回其退出代码

但是,WshShell对象可以执行您需要的操作

Dim oSHELL, batchFile, user, password, exitCode
    Set oSHELL = VBA.CreateObject("WScript.Shell")
    batchFile="myBatchFile.cmd"
    user="myUserName"
    password="this is my password"

    ' Quote strings to avoid problems with spaces
    ' The arguments are WhatToRun, WindowStyle (0=hide), WaitForProcessToEnd
    exitCode = oSHELL.Run(""""+batchFile+""" """+user+""" """+password+"""", 0, True)

ERRORLEVEL
变量的值随每次命令执行而变化(或多或少)。因此,当批处理文件中的代码执行时,每个命令都会生成一个更改。您需要存储该值以供以后处理,或者在您的情况下,在每个步骤中使用相应的值退出:

SET "username=%~1"
SET "password=%~2"

rem This will be tested for a errorlevel value of 1 or greater. In this case, 
rem no further processing will be done if errors found, so we return our own 
rem value to the calling process

net use "\\gessel\toys\name" %password% /user:shops\%username% 
if errorlevel 1 exit /b 100

rem We are connected, and will disconnect no matter if xcopy worked or failed
rem So, the result of the command will be stored to later return the adecuate value

Xcopy "\\gessel\toys\name\babytoys" "%appdata%\shops" /S /E
set "exitCode=%errorlevel%"

net use "\\gessel\toys\name\babytoys" /delete

EXIT /B %exitCode%
现在,在vba代码中,可以测试
error
变量的值100(我们从网络使用中的错误返回的值)或xcopy返回的任何值

现在我们有了一个工作批处理文件,让我们使用Excel

不,VBA中的
Shell
函数不能满足您的要求。
Shell
的返回值是正在运行的进程的id
Shell
不要等待进程结束,因此它无法返回其退出代码

但是,WshShell对象可以执行您需要的操作

Dim oSHELL, batchFile, user, password, exitCode
    Set oSHELL = VBA.CreateObject("WScript.Shell")
    batchFile="myBatchFile.cmd"
    user="myUserName"
    password="this is my password"

    ' Quote strings to avoid problems with spaces
    ' The arguments are WhatToRun, WindowStyle (0=hide), WaitForProcessToEnd
    exitCode = oSHELL.Run(""""+batchFile+""" """+user+""" """+password+"""", 0, True)

谢谢我已经按照你的建议修改了代码。但我有另一个问题。如果我单独运行批处理文件,看起来不错。但是从代码中,它总是复制文件,即使我提供了错误的数据。你能在这方面提供帮助吗?@viji,我正在测试,我无法使用无效数据执行
net use
命令,而无法获得错误级别集。设置errorlevel后,批处理文件将在网络使用后在线退出,并且不会复制任何文件。您可以在您的系统中尝试使用命令行中的无效数据执行该命令,并查看它是否设置了errorlevel及其设置的值吗?我已经尝试过了。看起来不错。但是这一行执行“exitCode=oSHELL.Run”(““+batchFile+”)“+user+”“+password+”,0,True)”花费了很多时间,我的意思是它不会返回到应用程序。是否可以设置等待或睡眠时间,直到批处理文件执行?但如果我单独运行批处理文件,则需要2或3秒。你知道吗?@viji,函数调用中的最后一个参数(
True
)表示在批处理文件完成之前不应返回调用。如果它不等待被调用的进程结束,则无法访问退出代码(它尚未完成)。你认为它应该有什么样的行为?谢谢。我已经按照你的建议修改了代码。但我有另一个问题。如果我单独运行批处理文件,看起来不错。但是从代码中,它总是复制文件,即使我提供了错误的数据。你能在这方面提供帮助吗?@viji,我正在测试,我无法使用无效数据执行
net use
命令,而无法获得错误级别集。设置errorlevel后,批处理文件将在网络使用后在线退出,并且不会复制任何文件。您可以在您的系统中尝试使用命令行中的无效数据执行该命令,并查看它是否设置了errorlevel及其设置的值吗?我已经尝试过了。看起来不错。但是这一行执行“exitCode=oSHELL.Run”(““+batchFile+”)“+user+”“+password+”,0,True)”花费了很多时间,我的意思是它不会返回到应用程序。是否可以设置等待或睡眠时间,直到批处理文件执行?但如果我单独运行批处理文件,则需要2或3秒。你知道吗?@viji,函数调用中的最后一个参数(
True
)表示在批处理文件完成之前不应返回调用。如果它不等待被调用的进程结束,则无法访问退出代码(它尚未完成)。你认为它应该有什么样的行为?