使用 Excel VBA 运行 CMD 命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44163233/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 12:38:10  来源:igfitidea点击:

Using Excel VBA to run a CMD command

excelvbaexcel-vbacmd

提问by BAW331

I would like to use Excel VBA to run a CMD command.

我想使用 Excel VBA 来运行 CMD 命令。

The basic cmd command is:

基本的cmd命令是:

"C:\Program Files\example program.exe" -w C:\Program files\outputfile.file "dir1" "dir2" "dir n+1"

The first part is the location of the program that will merge the files together. The second part is the file location of the outputted merged files. And the "dir1".... is the files that will be merged together.

第一部分是将文件合并在一起的程序的位置。第二部分是输出的合并文件的文件位置。而“dir1”.... 是将被合并在一起的文件。

I have code that lists the files to be merged but struggling to get the CMD code to get it so it does what I want as mentioned above. I have tried the following:

我有列出要合并的文件的代码,但正在努力获取 CMD 代码以获取它,因此它可以执行我想要的如上所述的操作。我尝试了以下方法:

   Sub RunCMD()

        Dim wsh As Object
        Set wsh = VBA.CreateObject("WScript.Shell")
        Dim waitOnReturn As Boolean: waitOnReturn = True
        Dim windowStyle As Integer: windowStyle = 1
        Dim locationofprogram as string
       'dir of program that will do the merge
        Dim outputfolder as string
       'dir of where the merged file will go
        Dim listoffiles as string
       'list of files to be merged

        wsh.Run "cmd.exe /S /C locationofprogram & " " & "-w" & " " & outputfolder & " " & listoffiles, windowStyle, waitOnReturn

    End Sub

Thanks for the help!

谢谢您的帮助!

采纳答案by Andreas

You can always create a .bat file, run the file, then delete it.

您始终可以创建一个 .bat 文件,运行该文件,然后将其删除。

MyFile = "C:\Bat-File\Bat-File.bat"

fnum = FreeFile()
Open MyFile For Output As #fnum
Print #fnum, "C:\Program Files\example program.exe -w C:\Program files\outputfile.file dir1 dir2 dir n+1"    
Close #fnum

Shell MyFile, vbNormalFocus

' wait 10 seconds to let bat file finnish
newHour = Hour(Now()) 
newMinute = Minute(Now()) 
newSecond = Second(Now()) + 10 
waitTime = TimeSerial(newHour, newMinute, newSecond) 
Application.Wait waitTime

' delete bat-file
kill MyFile

I can't test that example program runs without the " around the dir′s so you have to see what happens.
But if it does not work you need to double enclose the " or use & CHR(34) &

我无法测试示例程序在目录周围没有 " 的情况下运行,所以你必须看看会发生什么。
但如果它不起作用,你需要将 " 或使用双括起来& CHR(34) &

回答by CLR

If in doubt, send the text to the Immediate Window to see if it still matches the syntax using:

如果有疑问,请将文本发送到立即窗口以查看它是否仍然与使用的语法匹配:

 Debug.Print "cmd.exe /S /C " & locationofprogram & " " & "-w" & " " & outputfolder & " " & listoffiles

If that works, use:

如果可行,请使用:

 wsh.Run "cmd.exe /S /C " & locationofprogram & " " & "-w" & " " & outputfolder & " " & listoffiles, windowStyle, waitOnReturn