Launching Programs Or Files From Your Application

The cool thing about this code is that if a file does not have an associated program, then it will run the “Open With” window that allows the user to pick the program they want to file opened with. If the ShellExecute fails, then the code generates a error message box letting the user know exactly what went wrong.

How To Use LaunchFile

With the code below, I have done all the hard work. All you need to do is call it. The parameters are:

  • File – Full path along with the program or document name.
  • Verb (optional) – With the ShellExecute you can either open the program or file or have it print. Use “Open” or “Print” to let ShellExecute what you want to do. Leaving it blank will open the program or file.
  • Parameters (optional) – You can define command line parameters here for the program you are running.
  • ParenthWnd (optional) – Here is where you set the hWnd handle of the Visual Basic Form or UserControl you are calling this from.

There are two other routines here to strip out the path and file name needed to run ShellExecute.

Declare

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
(ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Code

 

Public Sub LaunchFile(File As String, Optional Verb As String, _
Optional Parameters As String, Optional ParenthWnd As Long)
Dim sFile As String
Dim sPath As String
Dim lReturn As Long
Dim sMessage As String
 
    'Test incomming values
    If Len(Verb) = 0 Then
        Verb = "Open"
    End If
    If Len(Parameters) = 0 Then
        Parameters = vbNullString
    End If
    'Strip out the componets of the file info we need
    sFile = StripPath(File)
    sPath = StripFile(File)
    lReturn = ShellExecute(ParenthWnd, Verb, sFile, Parameters, sPath, vbNormalFocus)
    'Check to make sure the shell went okay
    Select Case lReturn
        Case Is = 31
            'There is no associated program for the specified file type
            'Give the user the choice on how to open the file
            sFile = "rundll32.exe shell32.dll,OpenAs_RunDLL " & sPath & sFile
                        lReturn = Shell(sFile, vbNormalFocus)
        Case 0
            sMessage = "System is out of memory, executable file is corrupt, or relocations are invalid."
        Case 2
            sMessage = "File was not found."
                Case 3
            sMessage = "Path was not found."
        Case 5
            sMessage = "Attempt was made to dynamically link to a task, or there was a sharing or network-protection error."
        Case 6
            sMessage = "Library required separate data segments for each task."
        Case 8
            sMessage = "There was insufficient memory to start the application."
        Case 10
            sMessage = "Windows version was incorrect."
        Case 11
            sMessage = "Executable file was invalid. Either it was not a Windows application or there was an error in the .EXE image."
        Case 12
            sMessage = "Application was designed for a different operating system."
        Case 13
            sMessage = "Application was designed for MS-DOS 4.0."
        Case 14
            sMessage = "Type of executable file was unknown."
        Case 15
            sMessage = "Attempt was made to load a real-mode application (developed for an earlier version of Windows)."
        Case 16
            sMessage = "Attempt was made to load a second instance of an executable file containing multiple data segments that were not marked read-only."
        Case 19
            sMessage = "Attempt was made to load a compressed executable file. the file must be decompressed before it can be loaded."
        Case 20
            sMessage = "Dynamic-link library (DLL) file was invalid. One of the DLLs required to run this application was corrupt."
               Case 21
            sMessage = "Application requires Microsoft Windows 32-bit extensions."
        Case Else
    End Select
    If Len(sMessage) > 0 Then
        'Display Error
        MsgBox sMessage, vbOKOnly + vbInformation, "Motiva"
    End If
End Sub

Function removes path from fully-qualified file name, returns file name only.

Public Function StripPath(PathFile As String) As String
Dim iFind As Integer
Dim iStart As Integer

    StripPath = PathFile
    iFind = InStr(PathFile, "\")
    Do While iFind
        iStart = iFind
        iFind = InStr(iStart + 1, PathFile, "\")
    Loop
    If iStart > 0 Then
        StripPath = Mid(PathFile, iStart + 1)
    End If
End Function

Function removes file name from fully-qualified file name, returns path only.

Public Function StripFile(PathFile As String) As String
Dim lStart As Long
Dim lPos As Long
Dim lLastAt As Long
    'Look for the last "\"
    Do
        lStart = lPos + 1
        lPos = InStr(lStart, PathFile, "\")
    Loop While lPos
    lLastAt = lStart - 1
    'Get all to the left
    StripFile = Left(PathFile, lLastAt)
End Function

Sample Code

LaunchFile "c:\windows\notepad.exe", , , Me.hwnd

 

Tip By: David McCarter


Discover more from dotNetTips.com

Subscribe to get the latest posts sent to your email.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.