Attribute VB_Name = "modSuperShell" Option Explicit '============================================================================================================= ' ' modSuperShell Module ' -------------------- ' ' Created By : Kevin Wilson ' http://www.TheVBZone.com ( The VB Zone ) ' http://www.TheVBZone.net ( The VB Zone .net ) ' ' Last Update : February 26, 2003 ' Created On : June 01, 2000 ' ' VB Versions : 5.0 / 6.0 ' ' Requires : NOTHING ' ' Description : Shell_Super and SuperShell are two alternatives to the default Visual Basic "Shell()" ' function. These two offer the developer much more flexiblity and reliability when ' starting other programs from within Visual Basic than the Shell() function can offer. ' ' NOTE : For more powerfull opening and closing of programs and more control over the process ID and/or ' thread ID of the started program(s), use the "OpenProgram" function of the modOpenAndClose.bas ' module. ' '============================================================================================================= ' ' LEGAL: ' ' You are free to use this code as long as you keep the above heading information intact and unchanged. Credit ' given where credit is due. Also, it is not required, but it would be appreciated if you would mention ' somewhere in your compiled program that that your program makes use of code written and distributed by ' Kevin Wilson (www.TheVBZone.com). Feel free to link to this code via your web site or articles. ' ' You may NOT take this code and pass it off as your own. You may NOT distribute this code on your own server ' or web site. You may NOT take code created by Kevin Wilson (www.TheVBZone.com) and use it to create products, ' utilities, or applications that directly compete with products, utilities, and applications created by Kevin ' Wilson, TheVBZone.com, or Wilson Media. You may NOT take this code and sell it for profit without first ' obtaining the written consent of the author Kevin Wilson. ' ' These conditions are subject to change at the discretion of the owner Kevin Wilson at any time without ' warning or notice. Copyright© by Kevin Wilson. All rights reserved. ' '============================================================================================================= ' Constants - ShellExecute.nShowCmd Public Enum ShowStates SW_HIDE = 0 ' Hides the window and activates another window. SW_MAXIMIZE = 3 ' Maximizes the specified window. SW_MINIMIZE = 6 ' Minimizes the specified window and activates the next top-level window in the z-order. SW_RESTORE = 9 ' Activates and displays the window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when restoring a minimized window. SW_SHOW = 5 ' Activates the window and displays it in its current size and position. SW_SHOWDEFAULT = 10 ' Sets the show state based on the SW_ flag specified in the STARTUPINFO structure passed to the CreateProcess function by the program that started the application. An application should call ShowWindow with this flag to set the initial show state of its main window. SW_SHOWMAXIMIZED = 3 ' Activates the window and displays it as a maximized window. SW_SHOWMINIMIZED = 2 ' Activates the window and displays it as a minimized window. SW_SHOWMINNOACTIVE = 7 ' Displays the window as a minimized window. The active window remains active. SW_SHOWNA = 8 ' Displays the window in its current state. The active window remains active. SW_SHOWNOACTIVATE = 4 ' Displays a window in its most recent size and position. The active window remains active. SW_SHOWNORMAL = 1 ' Activates and displays a window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when displaying the window for the first time. End Enum ' Constants = ShellExecute.strAction Public Enum ShowActions SA_Open = 0 ' Opens the file specified by the strWhatToShell parameter. The file can be an executable file, a document file, or a folder. SA_Explore = 1 ' Explores the folder specified by strWhatToShell. SA_Edit = 2 ' Launches an editor and opens the document for editing. If strWhatToShell is not a document file, the function will fail. SA_Print = 3 ' Prints the document file specified by strWhatToShell. If strWhatToShell is not a document file, the function will fail. SA_Properties = 4 ' Displays the file or folder's properties. SW_Find = 5 ' Initiates a search starting from the specified directory. SW_Play = 6 ' Plays audio files such as WAV, MID, and RMI files. End Enum ' Constants - ShellExecute Return Codes (Errors) Public Const ERROR_OOM = 0 ' The operating system is out of memory or resources. Public Const ERROR_FILE_NOT_FOUND = 2 ' The specified file was not found. Public Const ERROR_PATH_NOT_FOUND = 3 ' The specified path was not found. Public Const ERROR_BAD_FORMAT = 11 ' The .exe file is invalid (non-Win32® .exe or error in .exe image). Public Const SE_ERR_ACCESSDENIED = 5 ' The operating system denied access to the specified file. Public Const SE_ERR_ASSOCINCOMPLETE = 27 ' The file name association is incomplete or invalid. Public Const SE_ERR_DDEBUSY = 30 ' The DDE transaction could not be completed because other DDE transactions were being processed. Public Const SE_ERR_DDEFAIL = 29 ' The DDE transaction failed. Public Const SE_ERR_DDETIMEOUT = 28 ' The DDE transaction could not be completed because the request timed out. Public Const SE_ERR_DLLNOTFOUND = 32 ' The specified dynamic-link library was not found. Public Const SE_ERR_FNF = 2 ' The specified file was not found. Public Const SE_ERR_NOASSOC = 31 ' There is no application associated with the given file name extension. This error will also be returned if you attempt to print a file that is not printable. Public Const SE_ERR_OOM = 8 ' There was not enough memory to complete the operation. Public Const SE_ERR_PNF = 3 ' The specified path was not found. Public Const SE_ERR_SHARE = 26 ' A sharing violation occurred. ' Subs / Functions Public Declare Function ShellExecute Lib "SHELL32.DLL" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal strAction As String, ByVal strWhatToShell As String, ByVal strParameters As String, ByVal strDefaultDir As String, ByVal nShowCmd As Long) As Long '============================================================================================================= ' SuperShell ' ' Purpose: ' This function starts the specified program, goes to the specified URL using the default browser, opens the ' specified document, prints the specified document, explores to the specified path, or opens an Email to the ' specified address using the default Email program (depending on the "ShowAction" parameter) ' ' Param Use ' ------------------------------------ ' File_Email_URL Specifies the folder, file, document, URL, or Email to work with ' Parameters Optional. Specifies any parameters to pass to the file to execute ' ShowAction Optional. Specifies the action to take on the file (open, print, edit, etc.) ' Not all actions are applicable to all file types. ' ShowSate Optional. How to start the specified program ' DefaultDirectory Optional. The default directory to use ' WindowHandle Optional. Handle to a parent window. This window receives any message boxes ' that an application produces. ' ShowErrorMsg Optional. If set to TRUE and an error occurs, a message box will be displayed ' describing the error that occured ' Return_ErrNum Optional. If an error occurs while executing this function, this parameter returns ' the number of the error. ' Return_ErrSrc Optional. If an error occurs while executing this function, this parameter returns ' the source of the error. ' Return_ErrDesc Optional. If an error occurs while executing this function, this parameter returns ' the description of the error. ' ' Return: ' ------- ' Returns TRUE if the function completed successfully ' Returns FALSE if an error occured while executing this function ' ' Example Usage: ' -------------- ' ' SuperShell "NOTEPAD.EXE", , SA_Open, SW_SHOWNORMAL, "C:\WINDOWS\", Form1.hWnd, True ' Start a program ' SuperShell "mailto:BillGates@Microsoft.com", , SA_Open, SW_SHOWNORMAL, , , True ' Send Email ' SuperShell "http://www.microsoft.com", , SA_Open, SW_SHOWNORMAL, , , True ' Surf to URL ' SuperShell "C:\", , SA_Explore, SW_SHOWNORMAL, , , True ' Open Windows Explorer to C: ' SuperShell "C:\MyDoc.doc", , SA_Print, SW_SHOWNORMAL, , , True ' Print file ' SuperShell "C:\Funny.wav", , SW_Play, SW_SHOWNORMAL, , , True ' Play audio file '============================================================================================================= Public Function SuperShell(ByVal File_Email_URL As String, _ Optional ByVal Parameters As String = vbNullString, _ Optional ByVal ShowAction As ShowActions = SA_Open, _ Optional ByVal ShowSate As ShowStates = SW_SHOWNORMAL, _ Optional ByVal DefaultDirectory As String = vbNullString, _ Optional ByVal WindowHandle As Long = -1, _ Optional ByVal ShowErrorMsg As Boolean = True, _ Optional ByRef Return_ErrNum As Long, _ Optional ByRef Return_ErrSrc As String, _ Optional ByRef Return_ErrDesc As String) As Boolean On Error GoTo ErrorTrap Dim ReturnValue As Long Dim strVerb As String Dim strErrMsg As String Return_ErrNum = 0 Return_ErrSrc = "" Return_ErrDesc = "" ' Make sure the parameters passed are valid File_Email_URL = Trim(File_Email_URL) If File_Email_URL = "" Then Err.Raise -1, "SuperShell()", "No file, Email, or URL specified to open" If WindowHandle = -1 Then WindowHandle = App.hInstance If Right(File_Email_URL, 1) <> Chr(0) Then File_Email_URL = File_Email_URL & Chr(0) If Right(DefaultDirectory, 1) <> Chr(0) And DefaultDirectory <> vbNullString Then DefaultDirectory = DefaultDirectory & Chr(0) If Right(Parameters, 1) <> Chr(0) And Parameters <> vbNullString Then Parameters = Parameters & Chr(0) ' Get the verb that will be used to specify the action to take Select Case ShowAction Case SA_Open: strVerb = "open" Case SA_Explore: strVerb = "explore" Case SA_Edit: strVerb = "edit" Case SA_Print: strVerb = "print" Case SA_Properties: strVerb = "properties" Case SW_Find: strVerb = "find" Case SW_Play: strVerb = "play" Case Else: strVerb = "open" End Select ' Start the file, document, URL, etc. ReturnValue = ShellExecute(WindowHandle, strVerb, File_Email_URL, Parameters, DefaultDirectory, ShowSate) ' Check if there was an error starting the program If ReturnValue <= 32 Then Select Case ReturnValue Case ERROR_OOM: strErrMsg = "The operating system is out of memory or resources." Case ERROR_FILE_NOT_FOUND: strErrMsg = "The specified file was not found." Case ERROR_PATH_NOT_FOUND: strErrMsg = "The specified path was not found." Case ERROR_BAD_FORMAT: strErrMsg = "The .exe file is invalid (non-Win32® .exe or error in .exe image)." Case SE_ERR_ACCESSDENIED: strErrMsg = "The operating system denied access to the specified file." Case SE_ERR_ASSOCINCOMPLETE: strErrMsg = "The file name association is incomplete or invalid." Case SE_ERR_DDEBUSY: strErrMsg = "The DDE transaction could not be completed because other DDE transactions were being processed." Case SE_ERR_DDEFAIL: strErrMsg = "The DDE transaction failed." Case SE_ERR_DDETIMEOUT: strErrMsg = "The DDE transaction could not be completed because the request timed out." Case SE_ERR_DLLNOTFOUND: strErrMsg = "The specified dynamic-link library was not found." Case SE_ERR_FNF: strErrMsg = "The specified file was not found." Case SE_ERR_NOASSOC: strErrMsg = "There is no application associated with the given file name extension. This error will also be returned if you attempt to print a file that is not printable." Case SE_ERR_OOM: strErrMsg = "There was not enough memory to complete the operation." Case SE_ERR_PNF: strErrMsg = "The specified path was not found." Case SE_ERR_SHARE: strErrMsg = "A sharing violation occurred." Case Else: strErrMsg = "Unknown Error" End Select Err.Raise ReturnValue, "ShellExecute()", strErrMsg Else SuperShell = True End If Exit Function ErrorTrap: Return_ErrNum = Err.Number Return_ErrSrc = Err.Source Return_ErrDesc = Err.Description Err.Clear If ShowErrorMsg = True Then MsgBox "The following error occured while trying to open the specified file, folder, URL, Email, or document:" & Chr(13) & Chr(13) & _ "Error Number = " & CStr(Return_ErrNum) & Chr(13) & _ "Error Source = " & Return_ErrSrc & Chr(13) & _ "Error Description = " & Return_ErrDesc, vbOKOnly + vbExclamation, " Error" End If End Function