运行 来自 Excel 使用 VBA 的 bat 文件

Run bat file from Excel using VBA

我有 test.bat 文件包含脚本:

copy host_name table_name -p table_name -t "file.csv"

通常当我点击它时,它工作正常。现在,我想使用 vba.

从 Excel 运行 test.bat 文件
strPath = ws1.Range("G2").value & "\" 'Directory to folder with bat
Shell strPath & "\test.bat", vbNormalFocus

有些不对劲,因为我只看到snapshot/clip: 好像有东西在一秒钟内打开和关闭...

我不确定你使用的 VBA 和 shell,但试试这样的东西

Dim winShell As Object: Set winShell = CreateObject("WScript.Shell")

    Dim WaitOnReturn As Boolean: WaitOnReturn = False
    Dim windowStyle As Integer: windowStyle = 1

    'Run the desired pair of shell commands in command prompt.
    Call winShell.Run("cmd /k " & command1 & " & " & command2, windowStyle, WaitOnReturn)

    'Release pointer to the command prompt.
    Set winShell = Nothing

只需用您的命令替换我的命令,让我们看看它是否有效

编辑:为了完成,我的命令看起来像这样

Dim command1 As String: command1 = "cd /d" & projectDirectoryCell.Value

Dim command2 As String: command2 = "create_sensi.bat"

刚刚解决了它:

ChDir ThisWorkbook.Path & "\folder\"
Shell ThisWorkbook.Path & "\folder\test.bat", vbNormalFocus