The API function ShellExecuteA is one of the most useful API's I use when programing and I have used this one for some time now. If you look @ this function you would realise it is similar to VB/VBA Shell function. So why do I use it, and for that matter why would I want you to use this function. Lets look @ each one.
VBA - Shell (From help)
Runs an executable program and returns a Variant (Double) representing the program's task ID if successful, otherwise it returns zero.
API - ShellExecuteA
This function opens or prints a specified file using the associated program. The file can be an executable file or a document file.
ShellExecuteA uses the built in file association data stored in the registry to determine which process will be passed to the CreateProcess API. So if you feed it *.xls file and Excel is installed, it will execute Excel accordingly.
OK, fairly similar except for two things (or a least the ones I believe are important, as you could also argue that it also allows you to "Print", "Play" and "Explore" (I'll get to these latter) -
1)associated program. That's the real plus with this API, you don't need to know what program to use in order to launch a document as long as the document is associated with a launcher application. Windows file associations provide the easiest way to launch applications by passing just the path and filename of the document to open. You could argue that Shell will do this to, BUT you need to know where the executable program is located and that's
(See here for a way to get a files Executable program path) where this API comes in handy, no need to know this especially for a clients machine. Also, to assume everyone uses Internet Explorer as there browser, or that a clients emailer is Outlook is like assuming everyone speaks English! Whilst the World is becoming borderless we must still consider that everyone is different and that a PC is exactly that a Personal Computer, people will use different programs.
2) The other plus is that in some instances calling other methods in Excel eg FollowHyperlink you will be given a system generated message asking you weather to allow this operation. This API over comes this.
Let this API do the work for you in launching the users default browser, emailer etc. Have a look here for how to use the persons default emailer using this API
ShellExecuteA takes six parameters, now you can see why I usually format my API declarations as above as you can easily see which is which i.e. you can tell at a glance
The function name, it's real API name the parameters and type and also what the return is.!
1) The first parameter, hWnd, is the Window handle to a parent window, usually your applications handle or failing this then just set it to your desktop (0&). This window receives any message boxes that an application produces. For example, an application may report an error by producing a message box.
2) The second parameter is lpOperation - the action that will take place on execution. These are passed as strings:
"Open" - The function opens the file specified by the lpFile parameter. The file can be an executable file or a document file. It can also be a folder. Note if your leave this blank "" it will use the default operation, usually "Open"
"Print" The function prints the file specified by lpFile. The file should be a document file. If the file is an executable file, the function opens the file, as if "open" had been specified. Using this option opens the document, prints it THEN closes it. Very useful for those documents you need to just print! I use this to print Documents, spreadsheets and text files etc from within an application. TIP: Using the API to print is similar to right clicking a document within explorer and selecting print! This is what I use for printing files within a list in a useform.
"Explore" The function explores the folder specified by lpFile.
In addition, "Play" can also be specified for methods supporting a play function, such as sound files, or "Show" as in Power Point Slides PPS
Also of NOTE: are other system operations eg "Find" which will bring up the Windows Find/Search dialog see here. + Edit - Launches an editor and opens the document specified by lpFile for editing. + Properties - Displays the properties of the file specified by lpFile.
The lpOperation parameter can be also be NULL (0&). In that case, the function opens the file specified by lpFile ie the default is to open the document/Application.
TIP:
One way to find out the operations performed is via your Explorer window.
XP - Tools > Folder Options > File Types (Tab) You should get this
3) The third param is lpFile - a string that specifies the file to open or print or the folder to open or explore. The function can open an executable file or a document file. The function can print a document file.
4) The lpParameters member is a special case. If the lpFile parameter specifies an executable file, lpParameters is a string that specifies the parameters to be passed to the application. If lpFile specifies a document file, lpParameters should be NULL (0&).
5) The lpDirectory member is simply a string that specifies the default directory.
6) The last parameter is the nShowCmd member. Like other show commands, this specifies how the application is to be shown when it is opened. If this parameter is not specified, the application uses its default value.
If lpFile specifies a document file, nShowCmd should be zero.
Clicking the Advanced tab gives you this
As you can see, there are 5 of these for my Excel = XL2003
PrivateConst SW_HIDE AsLong = 0 '// Hides the window and activates another window. PrivateConst SW_NORMAL AsLong = 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. PrivateConst SW_SHOWMINIMIZED AsLong = 2 '// Activates the window and displays it as a minimized window. PrivateConst SW_MAXIMIZE AsLong = 3 '// Activates the window and displays it as a maximized window. PrivateConst SW_SHOWNOACTIVATE AsLong = 4 '// Displays the window as a minimized window. The active '// window remains active. PrivateConst SW_SHOW AsLong = 5 '// Activates the window and displays it in its current size '// and position. PrivateConst SW_MINIMIZE AsLong = 6 '// Minimizes the specified window and activates the next '// top-level window in the Z order. PrivateConst SW_SHOWMINNOACTIVE AsLong = 7 '// Displays the window in its current state. The active '// window remains active. PrivateConst SW_SHOWNA AsLong = 8 '// Displays the window in its current size and position. '// This value is similar to SW_SHOW, '// except the window is not activated. PrivateConst SW_RESTORE AsLong = 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. PrivateConst SW_SHOWDEFAULT AsLong = 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.
PrivateConst SW_FORCEMINIMIZE AsLong = 11 '// Windows 2000/XP: Minimizes a window, even if the '// thread that owns the window is hung. '// This flag should only be used when minimizing '// windows from a different thread.
Return Value:
If the function succeeds, the return value is cast as a HINSTANCE for backward compatibility with 16-bit Windows applications. It is not a true HINSTANCE, the only thing that can be done with the returned HINSTANCE is to cast it to an int and compare it with the value 32 or one of the error codes below, which indicates the function failed.