Shell32 - #03
ShellExecuteA
Ivan F Moala
Can Do
Visits here
Goto Guest book sign in page [Home]
Greatest place to be
Thanks for visiting my site lucky visitor:
Copyright © 2002. XcelFiles. All Rights Reserved Ivan F Moala
Tell a friend about this page
Google
Search WWW Search My Site!

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

Also here @ Ozgrid for a Browse routine (NOT Shell execute BUT a part of the shell32)

Brief:

Public 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

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:






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

Why Donate?
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
The default operation is Bold = Open
'////////////////////////
'// Constants nShowCmd //
'////////////////////////

Private Const SW_HIDE As Long = 0
'// Hides the window and activates another window.
Private Const SW_NORMAL As Long = 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.
Private Const SW_SHOWMINIMIZED As Long = 2
'// Activates the window and displays it as a minimized window.
Private Const SW_MAXIMIZE As Long = 3
'// Activates the window and displays it as a maximized window.
Private Const SW_SHOWNOACTIVATE As Long = 4
'// Displays the window as a minimized window. The active
'// window remains active.
Private Const SW_SHOW As Long = 5
'// Activates the window and displays it in its current size
'// and position.
Private Const SW_MINIMIZE As Long = 6
'// Minimizes the specified window and activates the next
'// top-level window in the Z order.
Private Const SW_SHOWMINNOACTIVE As Long = 7
'// Displays the window in its current state. The active
'// window remains active.
Private Const SW_SHOWNA As Long = 8
'// Displays the window in its current size and position.
'// This value is similar to SW_SHOW,
'// except the window is not activated.
Private Const SW_RESTORE As Long = 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.
Private Const SW_SHOWDEFAULT As Long = 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.

Private Const SW_FORCEMINIMIZE As Long = 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.
This page was last updated on: 30 Nov 2003
Private Const SE_ERR_FNF = 2&
Private Const SE_ERR_PNF = 3&
Private Const SE_ERR_ACCESSDENIED = 5&
Private Const SE_ERR_OOM = 8&
Private Const ERROR_BAD_FORMAT = 11&
Private Const SE_ERR_SHARE = 26&
Private Const SE_ERR_ASSOCINCOMPLETE = 27&
Private Const SE_ERR_DDETIMEOUT = 28&
Private Const SE_ERR_DDEFAIL = 29&
Private Const SE_ERR_DDEBUSY = 30&
Private Const SE_ERR_NOASSOC = 31&
Private Const SE_ERR_DLLNOTFOUND = 32&
Here is a basic wrapper routine to use the Shell operations

Note: Rather then use the Constants I have deciphered them to give a discriptive text explanation.
Do you use Excel for all of your projects? A PDF converter would be a good investment. With this software, not only can you convert a PDF to Excel, but you can also convert PDF to Word and all kinds of other documents.