Found this function http://www.cpearson.com/excel/ShellAndWait.aspx
But would need also to capture output from the shell output. Any code suggestion ?
Found this function http://www.cpearson.com/excel/ShellAndWait.aspx
But would need also to capture output from the shell output. Any code suggestion ?
You could always redirect the shell output to a file, then read the output from the file.
Based on bburns.km's answer, I added passing input (using StdInput) to the executable during call. Just in case somebody stumbles upon this and has the same need.
Based on Andrew Lessard's answer, here's a function to run a command and return the output as a string -
Usage:
You can
CreateProcess
the application redirecting itsStdOut
to a pipe, then read that pipe directly; http://pastebin.com/CszKUpNSThis function provides a quick way to run a Command Line command, using the clipboard object:
Capture command-line output:
Example usage:
It uses the WShell
Run
command because it optionally allows for asynchronous execution, meaning it will wait for the command to finish running before VBA continues, which is important when involving the clipboard.It also utilizes a built-in but often-forgotten command line utility called
clip.exe
, in this case as a destination for the piped cmdline output.Clipboard manipulation requires a reference to the Microsoft Forms 2.0 library, which in this case I created with a Late-bound reference (which looks different since MS Forms - aka
fm20.dll
- is a Windows library, not VBA).Preserving Existing Clipboard Data:
In my case it was an issue that the function above wipes the existing clipboard data, so the function below is modified to retain & replace existing text on the clipboard.
If there is something other than text on the clipboard you'll be warned that it will be lost. Some heavy coding could allow for other/any type of clipboard data to be returned... but advanced clipboard manipulation is far more complex than most users realize, and I frankly don't have the need or desire to get into it. More info here.
Note that this in this method MS Forms is Early-Bound but could be changed if desired. (But remember as a general rule of thumb, late-binding generally doubles processing time.)
Example Usage: