windows - 使用 shell 命令在 vba 中执行批处理文件不起作用

标签 windows vba batch-file excel

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

.bat文件:

SET username=%1
SET password=%2
net use "\\gesvij\toys\names" %password% /user:shop\%username%
:copy
Xcopy "\\gesvij\toys\names" "%appdata%\Microsoft\Templates" /S /E
IF ERRORLEVEL 0 goto disconnect
goto end
:disconnect 
net use "\\gesvij\toys\names" /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("\\gesvij\toys\names\test.bat & un & pd")
end sub

现在我有以下问题:

1) 批处理文件未从 vba 代码正常运行。但是当我独立运行时,bat 文件可以正常工作。 2) 一旦文件复制到本地我如何断开批处理文件中的网络路径“\gesvij\toys\names”

有人问这个吗?

最佳答案

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

"\\gesvij\toys\names\test.bat " & un & pd

下面是我如何从 VBA 调用 .bat 文件

Sub CreateReleasePart()
    Dim batPath As String: batPath = "C:\Users\sl\Desktop\"

    call Shell(Environ$("COMSPEC") & " /c " & batPath & "\myFile.bat", vbNormalFocus)
End Sub

关于windows - 使用 shell 命令在 vba 中执行批处理文件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22380923/

相关文章:

.net - 迷你转储对 .NET 中的调试有用吗

excel - 我可以在 Excel VBA 中捕获并模拟 KeyDown 事件吗?

excel - 在 Excel 中从 URL 加载图像

php - 在 Windows 上安装 PHP Mongo 驱动程序

windows - 文件名包含两个空格的 exec.Command 失败

c - 在没有 WdfDeviceCreateDeviceInterface 的情况下为 Windows HID 驱动程序初始化设备接口(interface)

excel - VBA查找和替换文件路径的一部分

windows - 我如何使这个 MS 更新批量卸载程序工作?

windows-7 - 通过脚本更改 Windows (7) 监视器设置的方法

c# - 在批处理文件中包含一个批处理文件