使用excel VBA在命令提示符下执行命令 [英] Execute a command in command prompt using excel VBA

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

问题描述

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

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: emp"

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

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: emp", vbNormalFocus)

请检查.

推荐答案

S 参数本身不做任何事情.

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  

试试这样的东西

Call Shell("cmd.exe /S /K" & "perl a.pl c:	emp", vbNormalFocus)

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

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:	emp")



-编辑-
要等待命令完成,您必须执行类似@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:	emp", windowStyle, waitOnReturn

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆