使用excel VBA在命令提示符中执行命令

use*_*227 40 shell excel command-line vba excel-vba

我有一个固定的命令,我需要使用VBA传递给命令提示符,然后命令应该运行.例如"perl a.pl c:\ temp"

以下是我尝试使用的命令但它只是打开命令提示符并且不运行命令.

Call Shell("cmd.exe -s:" & "perl a.pl c:\temp", vbNormalFocus)

请检查.

Rip*_*ter 54

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  
Run Code Online (Sandbox Code Playgroud)

尝试这样的事情

Call Shell("cmd.exe /S /K" & "perl a.pl c:\temp", vbNormalFocus)
Run Code Online (Sandbox Code Playgroud)

您可能甚至不需要在此命令中添加"cmd.exe",除非您希望在运行此命令时打开命令窗口.Shell应该自己执行命令.

Shell("perl a.pl c:\temp")
Run Code Online (Sandbox Code Playgroud)



CNC中
等待命令完成,你将不得不做一些像@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
Run Code Online (Sandbox Code Playgroud)

  • 请记住,shell将在启动进程后返回.它不会等待你的Perl-Script完成,只是说. (5认同)