在vba中使用shell推荐执行批处理文件不工作



我已经创建了批处理文件从网络驱动器复制文件到本地。

。bat文件:

SET username=%1
SET password=%2
net use "\gesvijtoysnames" %password% /user:shop%username%
:copy
Xcopy "\gesvijtoysnames" "%appdata%MicrosoftTemplates" /S /E
IF ERRORLEVEL 0 goto disconnect
goto end
:disconnect 
net use "\gesvijtoysnames" /delete
goto end
:end

我尝试了下面的代码在vba中执行批处理文件。

sub ok_click
dim un as string
dim pd as string
un = " " + un_text.value
pd = " " + pd_text.value
call shell("\gesvijtoysnamestest.bat & un & pd")
end sub

现在我有以下问题:

1)批处理文件没有从vba代码正常运行。但是当我独立运行时,bat文件工作正常。2)一旦文件复制到本地,我如何断开批处理文件中的网络路径"gesvijtoysnames"

有人知道吗?

您没有正确添加un和pd变量。它们需要在引号之外,像这样:

"\gesvijtoysnamestest.bat " & un & pd

下面是如何从VBA中调用。bat文件
Sub CreateReleasePart()
    Dim batPath As String: batPath = "C:UsersslDesktop"
    call Shell(Environ$("COMSPEC") & " /c " & batPath & "myFile.bat", vbNormalFocus)
End Sub

最新更新