[Return to contents]
Last updated: 10 Nov 2002
Paul Bent submitted the following script which demonstrates the use of Windows API to launch another application, stating that it is much better than using the Shell function, as there is no need to know the path or name of the exe and no parsing problems.
'[Globals - Declarations] ShellExecute API '--- ShellExecute uses the shell to open or print a file or run a program. '--- Under Win 95/98, this function will also open a My Computer or Explorer window to a given directory. '--- If an executable program is specified, Windows will run that program. '--- If a document file is specified, Windows will open or print it using the associated program (whatever it happens to be). '--- If successful, the function returns a handle to the instance of the opened program '--- or (in the case of printing) a handle to the invoked DDE server application. '--- If unsuccessful, the function returns either 0 (meaning out of memory or resources) or one of the following error code flags: Declare Public 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 'hwnd The handle of the window calling the function. 'lpOperation The operation to perform on lpFile. "open" means open the file or run the program (or directory in Win 95/98). "print" means print 'the document. In Win 95/98, "explore" means open the directory in an Explorer window. The default is "open". 'lpFile The file to perform the operation on. 'lpParameters Any command-line parameters to pass to an opened application. 'lpDirectory The working directory for the operation. Public Const ERROR_FILE_NOT_FOUND = 2& 'The specified file could not be found. Public Const ERROR_PATH_NOT_FOUND = 3& 'The specified directory could not be found. Public Const ERROR_BAD_FORMAT = 11& 'The specified executable file (.EXE) was somehow invalid. Public Const SE_ERR_ACCESSDENIED = 5 'Win 95/98 only: Windows denied access to the specified file Public Const SE_ERR_ASSOCINCOMPLETE = 27 'The filename association is either incomplete Or invalid Public Const SE_ERR_DDEBUSY = 30 'The DDE action could not run because other DDE actions are in process Public Const SE_ERR_DDEFAIL = 29 'The DDE transaction failed Public Const SE_ERR_DDETIMEOUT = 28 'The DDE transaction was not completed because the request timed out Public Const SE_ERR_DLLNOTFOUND = 32 'Win 95/98 only: The specified DLL file was not found Public Const SE_ERR_FNF = 2 'Same as ERROR_FILE_NOT_FOUND Public Const SE_ERR_NOASSOC = 31 'There is no program associated with the specified type of file Public Const SE_ERR_OOM = 8 'Win 95/98 only: Windows has insufficient memory to perform the operation Public Const SE_ERR_PNF = 3 'Same as ERROR_PATH_NOT_FOUND Public Const SE_ERR_SHARE = 26 'A sharing violation occured Public Const SW_HIDE = 0 'Hide the opened window Public Const SW_MAXIMIZE = 3 'Maximize the opened window Public Const SW_MINIMIZE = 6 'Minimize the opened window Public Const SW_RESTORE = 9 'Restore the opened window (not maximized nor minimized) Public Const SW_SHOW = 5 'Show the opened window Public Const SW_SHOWMAXIMIZED = 3 'Show the opened window maximized Public Const SW_SHOWMINIMIZED = 2 'Show the opened window minimized Public Const SW_SHOWMINNOACTIVE = 7 'Show the opened window minimized but do not activate the it Public Const SW_SHOWNA = 8 'Show the opened window in its current state but do not activate it Public Const SW_SHOWNOACTIVATE = 4 'Show the opened window in its most recent size and position but do not activate it Public Const SW_SHOWNORMAL = 1 'Show the opened window and activate it (as usual) Then call it as follows Dim hWnd As Long 'Approach doc window handle Dim lngRtn As Long 'API function return value hWnd = CurrentWindow.GetHandle lngRtn = ShellExecute(hWnd, "open", "d:\my docs\mypowerpoint.ppt", "/s", "", SW_SHOWNORMAL)