vba 使用excel VBA在命令提示符下执行命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17956651/
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
Execute a command in command prompt using excel VBA
提问by user1699227
I have a fixed command which i need to pass to command prompt using VBA and then the command should run. e.g. "perl a.pl c:\temp"
我有一个固定的命令,我需要使用 VBA 将其传递给命令提示符,然后该命令应该运行。例如“perl a.pl c:\temp”
following is the command i am trying to use but it just opens command prompt and doesn't run the command.
以下是我尝试使用的命令,但它只是打开命令提示符而不运行命令。
Call Shell("cmd.exe -s:" & "perl a.pl c:\temp", vbNormalFocus)
Call Shell("cmd.exe -s:" & "perl a.pl c:\temp", vbNormalFocus)
Please check.
请检查。
回答by Ripster
The S parameter does not do anything on its own.
S 参数本身不做任何事情。
/S Modifies the treatment of string after /C or /K (see below)
/C Carries out the command specified by string and then terminates
/K Carries out the command specified by string but remains
Try something like this instead
试试这样的东西
Call Shell("cmd.exe /S /K" & "perl a.pl c:\temp", vbNormalFocus)
You may not even need to add "cmd.exe" to this command unless you want a command window to open up when this is run. Shell should execute the command on its own.
您甚至可能不需要向此命令添加“cmd.exe”,除非您希望在运行时打开命令窗口。Shell 应该自己执行命令。
Shell("perl a.pl c:\temp")
-Edit-
To wait for the command to finish you will have to do something like @Nate Hekman shows in his answer here
- 编辑-
要等待命令完成,您必须执行类似@Nate Hekman 在此处的回答中显示的操作
Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 1
wsh.Run "cmd.exe /S /C perl a.pl c:\temp", windowStyle, waitOnReturn