本文介绍了在命令提示符中使用excel VBA执行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



以下是我想要使用的命令,但它只是打开命令提示符,不运行命令。 / p>

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



请检查。

解决方案
  / S修改字符串在/ C或/ K后的处理方式b $ b / C执行由字符串指定的命令,然后终止
/ K执行由字符串指定的命令,但仍保持

尝试类似这样的

 调用Shell(cmd.exe / S / &perl a.pl c:\temp,vbNormalFocus)

添加cmd.exe到此命令,除非您希望在运行此命令时打开命令窗口。 Shell应该自己执行命令。

  Shell(perl a.pl c:\temp)





- 编辑 -

要等待命令完成,您必须在其答案中执行@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.Runcmd。 exe / S / C perl a.pl c:\temp,windowStyle,waitOnReturn


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"

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)

Please check.

解决方案

The S parameter does not do anything on its own.

/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.

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

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

这篇关于在命令提示符中使用excel VBA执行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 06:38