Results 1 to 14 of 14

Thread: [VBA] Run Application - Capture Output

Threaded View

  1. #1

    Thread Starter
    Fanatic Member dmaruca's Avatar
    Join Date
    May 2006
    Location
    Jacksonville, FL
    Posts
    577

    [VBA] Run Application - Capture Output

    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:
    1. 'Run an application, returning immediately to the caller.
    2. Public Sub RunApp(Optional cmd As String, Optional intMode As VbAppWinStyle = _
    3.     VbAppWinStyle.vbHide)
    4. 'Run an application, waiting for its completion before returning to the caller.
    5. Public Sub RunAppWait(Optional cmd As String, Optional intMode As VbAppWinStyle = _
    6.     VbAppWinStyle.vbHide)

    The next routine uses api.
    vb Code:
    1. 'Runs an application, waiting for its completion before returning to the caller.
    2. 'Screen output is captured and returned to the caller.
    3. 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:
    1. Sub ExtractTextDirect()
    2.     Dim cls As New clsRunApp
    3.     Dim s as String
    4.    
    5.     cls.Command = "C:\Path To\pdftotext.exe"
    6.     cls.AddParamater "-layout" 'preserve the layout of the text
    7.     'Surrounding quotes will be auto-added to this paramater since it has spaces.
    8.     cls.AddParamater "C:\super long path to pdf file\my pdf file.pdf"
    9.     'a hyphen as the next paramater directs output to stdout which we will capture
    10.     cls.AddParamater "-"
    11.    
    12.     s = cls.RunAppWait_CaptureOutput
    13.     Set cls = Nothing
    14.    
    15. 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.

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

    vb Code:
    1. cls.CheckForCommandNotExist = True
    2. cls.Command = "pdftotext.exe" 'ERROR
    3. cls.Command = "C:\Path To\pdftotext.exe" 'Yes
    4.  
    5. 'Ignore built in command builder. The stored command still exists.
    6. 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:
    1. Sub ErrorTest()
    2.     Dim cls As New clsRunApp
    3.    
    4.     On Error Resume Next
    5.     cls.CheckForCommandNotExist = True
    6.     cls.command = "c:\pdftotext.exe"
    7.     If Err.Number = cls.ErrNum(CommandPathNotFound) Then
    8.         MsgBox "pdtotext is not found"
    9.     End If
    10.    
    11.    
    12.     MsgBox cls.RunAppWait_CaptureOutput("klsdj /?")
    13.     If Err.Number = cls.ErrNum(ApiFailure) Then
    14.         MsgBox Replace("Api failure in {0}! ", "{0}", Err.Source) & Err.Description
    15.     End If
    16.    
    17.     'I thought this was cute.
    18.     MsgBox cls.RunAppWait_CaptureOutput("cmd /C echo Test complete.")
    19. 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.
    Attached Files Attached Files
    Last edited by dmaruca; Nov 14th, 2012 at 04:07 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width