使用 VBA 从 Excel 运行 bat 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37919707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run bat file from Excel using VBA
提问by 4est
I have test.batfile contains script:
我有test.bat文件包含脚本:
copy host_name table_name -p table_name -t "file.csv"
Normally when I click on it, it's working fine. Now, I want to run test.bat file from Excel using vba.
通常当我点击它时,它工作正常。现在,我想使用 vba 从 Excel 运行 test.bat 文件。
strPath = ws1.Range("G2").value & "\" 'Directory to folder with bat
Shell strPath & "\test.bat", vbNormalFocus
something is wrong, because I see only snapshot/clip: like something is open and close into one sec...
出了点问题,因为我只看到快照/剪辑:就像打开和关闭一秒钟一样......
回答by 4est
just resolved it:
刚刚解决了:
ChDir ThisWorkbook.Path & "\folder\"
Shell ThisWorkbook.Path & "\folder\test.bat", vbNormalFocus
回答by Lubo? Suk
im not sure with VBA and shell you using, but try something like this
我不确定您使用的 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
just replace my commands with your command(s) and lets see if its works
只需用您的命令替换我的命令,然后看看它是否有效
edit: just for completion my commands looks like this
编辑:只是为了完成我的命令看起来像这样
Dim command1 As String: command1 = "cd /d" & projectDirectoryCell.Value
Dim command2 As String: command2 = "create_sensi.bat"

