The new vbAccelerator Site - more VB and .NET Code and Controls
Source Code
3 Code Libraries &nbsp
 Using ShellEx from Visual Basic - Start any document from its filename only


 NOTE: this code has been superceded by the version at the new site.



 

Download the ShellEx project files (29kb)

The ShellExecute API function is a much better way of starting applications that VB's own Shell. It allows you to specify file names or internet resources as the file to shell, and will automatically find the associated executable and start it for you. Essentially its the same function as called by the Start->Run box.

[Shell Test Application Picture]



The source below provides a simple wrapper function for use of ShellExecute called ShellEx. The sample code in the download also includes a simple Browse For Folder implementation and uses some of the code from the Common Dialog/Direct project to implement a File Open dialog through API code.

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

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

Public Enum EShellShowConstants
&nbsp &nbsp essSW_HIDE = 0
&nbsp &nbsp essSW_MAXIMIZE = 3
&nbsp &nbsp essSW_MINIMIZE = 6
&nbsp &nbsp essSW_SHOWMAXIMIZED = 3
&nbsp &nbsp essSW_SHOWMINIMIZED = 2
&nbsp &nbsp essSW_SHOWNORMAL = 1
&nbsp &nbsp essSW_SHOWNOACTIVATE = 4
&nbsp &nbsp essSW_SHOWNA = 8
&nbsp &nbsp essSW_SHOWMINNOACTIVE = 7
&nbsp &nbsp essSW_SHOWDEFAULT = 10
&nbsp &nbsp essSW_RESTORE = 9
&nbsp &nbsp essSW_SHOW = 5
End Enum

Private Const ERROR_FILE_NOT_FOUND = 2&
Private Const ERROR_PATH_NOT_FOUND = 3&
Private Const ERROR_BAD_FORMAT = 11&
Private Const SE_ERR_ACCESSDENIED = 5&nbsp &nbsp &nbsp &nbsp ' access denied
Private Const SE_ERR_ASSOCINCOMPLETE = 27
Private Const SE_ERR_DDEBUSY = 30
Private Const SE_ERR_DDEFAIL = 29
Private Const SE_ERR_DDETIMEOUT = 28
Private Const SE_ERR_DLLNOTFOUND = 32
Private Const SE_ERR_FNF = 2&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp ' file not found
Private Const SE_ERR_NOASSOC = 31
Private Const SE_ERR_PNF = 3&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp ' path not found
Private Const SE_ERR_OOM = 8&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp ' out of memory
Private Const SE_ERR_SHARE = 26


Public Function ShellEx( _
&nbsp &nbsp &nbsp &nbsp ByVal sFIle As String, _
&nbsp &nbsp &nbsp &nbsp Optional ByVal eShowCmd As EShellShowConstants = essSW_SHOWDEFAULT, _
&nbsp &nbsp &nbsp &nbsp Optional ByVal sParameters As String = "", _
&nbsp &nbsp &nbsp &nbsp Optional ByVal sDefaultDir As String = "", _
&nbsp &nbsp &nbsp &nbsp Optional sOperation As String = "open", _
&nbsp &nbsp &nbsp &nbsp Optional Owner As Long = 0 _
&nbsp &nbsp ) As Boolean
Dim lR As Long
Dim lErr As Long, sErr As Long
&nbsp &nbsp If (InStr(UCase$(sFIle), ".EXE") 0) Then
&nbsp &nbsp &nbsp &nbsp eShowCmd = 0
&nbsp &nbsp End If
&nbsp &nbsp On Error Resume Next
&nbsp &nbsp If (sParameters = "") And (sDefaultDir = "") Then
&nbsp &nbsp &nbsp &nbsp lR = ShellExecuteForExplore(Owner, sOperation, sFIle, 0, 0, essSW_SHOWNORMAL)
&nbsp &nbsp Else
&nbsp &nbsp &nbsp &nbsp lR = ShellExecute(Owner, sOperation, sFIle, sParameters, sDefaultDir, eShowCmd)
&nbsp &nbsp End If
&nbsp &nbsp If (lR 32) Then
&nbsp &nbsp &nbsp &nbsp ShellEx = True
&nbsp &nbsp Else
&nbsp &nbsp &nbsp &nbsp ' raise an appropriate error:
&nbsp &nbsp &nbsp &nbsp lErr = vbObjectError + 1048 + lR
&nbsp &nbsp &nbsp &nbsp Select Case lR
&nbsp &nbsp &nbsp &nbsp Case 0
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp lErr = 7: sErr = "Out of memory"
&nbsp &nbsp &nbsp &nbsp Case ERROR_FILE_NOT_FOUND
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp lErr = 53: sErr = "File not found"
&nbsp &nbsp &nbsp &nbsp Case ERROR_PATH_NOT_FOUND
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp lErr = 76: sErr = "Path not found"
&nbsp &nbsp &nbsp &nbsp Case ERROR_BAD_FORMAT
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp sErr = "The executable file is invalid or corrupt"
&nbsp &nbsp &nbsp &nbsp Case SE_ERR_ACCESSDENIED
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp lErr = 75: sErr = "Path/file access error"
&nbsp &nbsp &nbsp &nbsp Case SE_ERR_ASSOCINCOMPLETE
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp sErr = "This file type does not have a valid file association."
&nbsp &nbsp &nbsp &nbsp Case SE_ERR_DDEBUSY
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp lErr = 285: sErr = "The file could not be opened because the target application is busy. Please try again in a moment."
&nbsp &nbsp &nbsp &nbsp Case SE_ERR_DDEFAIL
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp lErr = 285: sErr = "The file could not be opened because the DDE transaction failed. Please try again in a moment."
&nbsp &nbsp &nbsp &nbsp Case SE_ERR_DDETIMEOUT
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp lErr = 286: sErr = "The file could not be opened due to time out. Please try again in a moment."
&nbsp &nbsp &nbsp &nbsp Case SE_ERR_DLLNOTFOUND
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp lErr = 48: sErr = "The specified dynamic-link library was not found."
&nbsp &nbsp &nbsp &nbsp Case SE_ERR_FNF
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp lErr = 53: sErr = "File not found"
&nbsp &nbsp &nbsp &nbsp Case SE_ERR_NOASSOC
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp sErr = "No application is associated with this file type."
&nbsp &nbsp &nbsp &nbsp Case SE_ERR_OOM
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp lErr = 7: sErr = "Out of memory"
&nbsp &nbsp &nbsp &nbsp Case SE_ERR_PNF
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp lErr = 76: sErr = "Path not found"
&nbsp &nbsp &nbsp &nbsp Case SE_ERR_SHARE
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp lErr = 75: sErr = "A sharing violation occurred."
&nbsp &nbsp &nbsp &nbsp Case Else
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp sErr = "An error occurred occurred whilst trying to open or print the selected file."
&nbsp &nbsp &nbsp &nbsp End Select
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
&nbsp &nbsp &nbsp &nbsp Err.Raise lErr, , App.EXEName & ".GShell", sErr
&nbsp &nbsp &nbsp &nbsp ShellEx = False
&nbsp &nbsp End If

End Function


Here are a few samples of using the ShellEx function:

To open a the user's browser at a particular internet site
&nbsp &nbsp ShellEx "http://www.dogma.demon.co.uk", , , , , Me.hWnd

To print a document
&nbsp &nbsp ShellEx "C:\My Documents\Music\Brown Paper Bag.doc", , , , "print", Me.hWnd

To explore from a folder
&nbsp &nbsp ShellEx "C:\My Documents\Music", , , , "explore", Me.hWnd

Start VB, minimised, load a project and run it
&nbsp &nbsp ShellEx _
&nbsp &nbsp &nbsp &nbsp"C:\Program Files\DevStudio\Vb\vb5.exe",_
&nbsp &nbsp &nbsp &nbsp essSW_SHOWMINNOACTIVE, _
&nbsp &nbsp &nbsp &nbsp "/run_CStevemacVBDevelopBmpTilegBmpTile.vbg", _
&nbsp &nbsp &nbsp &nbsp,Me.hWnd



TopBack to top
Source Code - What We're About!Back to Source Code

&nbsp
 

About  Contribute  Send Feedback  Privacy

Copyright © 1998-1999, Steve McMahon ( steve@vbaccelerator.com). All Rights Reserved.
Last updated: 15 August 1999