This class includes 3 routines for shelling a program in Windows and an optional command-line builder.
The first 2 routines use shell().
vb Code:
'Run an application, returning immediately to the caller. Public Sub RunApp(Optional cmd As String, Optional intMode As VbAppWinStyle = _ VbAppWinStyle.vbHide) 'Run an application, waiting for its completion before returning to the caller. Public Sub RunAppWait(Optional cmd As String, Optional intMode As VbAppWinStyle = _ VbAppWinStyle.vbHide)
The next routine uses api.
vb Code:
'Runs an application, waiting for its completion before returning to the caller. 'Screen output is captured and returned to the caller. Public Function RunAppWait_CaptureOutput(Optional cmd As String) As String
The optional command-line builder can be explained using this example. I like to use this a lot for pdftotex.exe. This example shows you how to extract the text from a pdf file without using an intermediate text file. It is FIFO.
vb Code:
Sub ExtractTextDirect() Dim cls As New clsRunApp Dim s as String cls.Command = "C:\Path To\pdftotext.exe" cls.AddParamater "-layout" 'preserve the layout of the text 'Surrounding quotes will be auto-added to this paramater since it has spaces. cls.AddParamater "C:\super long path to pdf file\my pdf file.pdf" 'a hyphen as the next paramater directs output to stdout which we will capture cls.AddParamater "-" s = cls.RunAppWait_CaptureOutput Set cls = Nothing End Function
A note about the auto adding of quotes to paramaters: AddParamater has an optional paramater for using quotes. The default behavior will add quotes around paramaters with spaces, but not add any around paramaters without spaces.
The class also has an optional flag for checking if the command exists when you assign it to the Command property. This is useful only if you want your code to fail on the command assignment. Only use this if you provide the full path to the command.Code:'Use by AddParamater. See procedure header for explanation. Public Enum eQuote eQuote_Normal eQuote_ForceNone eQuote_ForceQuotes End Enum eQuote_Normal is discussed above. eQuote_ForceNone is for if your paramater has spaces but you do not want it surrounded by quotes. Useful for switches like -f 37. eQuote_ForceQuotes surrounds the paramater with quotes no matter what. So if you lump all your switches in one statement use this: cls.AddParamater "-o -f 37 -g", eQuote_ForceNone
vb Code:
cls.CheckForCommandNotExist = True cls.Command = "pdftotext.exe" 'ERROR cls.Command = "C:\Path To\pdftotext.exe" 'Yes 'Ignore built in command builder. The stored command still exists. MsgBox cls.RunAppWait_CaptureOutput("netstat /?")
I've included plenty of built-in error checking and documentationin the class file. Check it out if you want.
vb Code:
Sub ErrorTest() Dim cls As New clsRunApp On Error Resume Next cls.CheckForCommandNotExist = True cls.command = "c:\pdftotext.exe" If Err.Number = cls.ErrNum(CommandPathNotFound) Then MsgBox "pdtotext is not found" End If MsgBox cls.RunAppWait_CaptureOutput("klsdj /?") If Err.Number = cls.ErrNum(ApiFailure) Then MsgBox Replace("Api failure in {0}! ", "{0}", Err.Source) & Err.Description End If 'I thought this was cute. MsgBox cls.RunAppWait_CaptureOutput("cmd /C echo Test complete.") End Sub
You have probably seen these floating around, but I use these routines so often I finally decided to combine them in one place and wrap them in a class with a built-in command builder. Maybe you'll find it useful.
Edit 2-3-2010:
A bug was discovered in the CreateProcess call inside RunAppWait_CaptureOutput. This has been repaired.
Edit 11-14-2012
Modified to work with xcopy and other programs which require stdin. The copy attached to this post will not work with xcopy. See the update post here.




Reply With Quote