You can run another application or open a file by using the ShellExecute statement. This is basically the same as entering text into the Run window. First, declare this:
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
Some programs register its name, so that you only have to enter its application name (these also work in the Run window). Some of these are:
| Application Name | Text Used |
| Notepad |
Notepad |
| WordPad |
Wordpad |
| Word |
Winword |
| Excel |
Excel |
| Access |
MSAccess |
| Publisher |
MSPub |
| Paint |
MSPaint |
| Explorer |
Explorer |
| Internet Explorer |
Iexplore |
| MSDos Editor | edit |
So,
ShellExecute 0&, vbNullString,
"WinWord", vbNullString, vbNullString, vbNormalFocus
Runs Word, and
ShellExecute 0&, vbNullString, "C:\test.doc",
vbNullString, vbNullString, vbNormalFocus
opens C:\test.doc with its default viewer. Note that if the path you pass contains spaces, you need to surround it by quotes:
ShellExecute 0&, vbNullString, """C:\dir
with spaces\test.doc""", vbNullString, vbNullString,
vbNormalFocus
which is actually the equivalent of passing this:
"
C:\dir
with spaces\test.doc
"
The fourth parameter specifies any command line parameters you want to pass. For example, if you wanted to run notepad, and get it to open c:\test.doc, then you would do this:
ShellExecute 0&, vbNullString,
"notepad", "c:\test.doc", vbNullString, vbNormalFocus
The fifth parameter specifies the startup directory (ie the directory that that will be shown when you click open). This is the same as the StartIn parameter in a Shortcut.
The sixth parameter specifies the startup position, and can be one of the following:
| Constant | Value | Description |
| vbHide | 0 | Window is hidden and focus is passed to the hidden window. |
| vbNormalFocus | 1 | Window has focus and is restored to its original size and position. |
| vbMinimizedFocus | 2 | Window is displayed as an icon with focus. |
| vbMaximizedFocus | 3 | Window is maximized with focus. |
| vbNormalNoFocus | 4 | Window is restored to its most recent size and position. The currently active window remains active. |
| vbMinimizedNoFocus | 6 | Window is displayed as an icon. The currently active window remains active. |
The ShellExecute function will return 2 if the file is not found.
You can also use this statement for sending an email:
ShellExecute 0&, vbNullString, "mailto:support@vbweb.co.uk",
vbNullString, vbNullString, vbNormalFocus
or to open a URL:
ShellExecute 0&, vbNullString, "http://www.vbweb.co.uk",
vbNullString, vbNullString, vbNormalFocus
Nice tutorial, but you seem to have overlooked something. How do I launch My Computer? Explorer with no parameter opens My Documents, Explorer mycomp~1 and Explorer ""My Computer"" both say the directory doesn't exist. (This is on XP Pro BTW.)
is there a possibility for me to open a program to another pc. im thinking like a server client program where the server opens the client or other program from the other terminal...??
I want my program to wait till Shell or shellexecute command finishes zipping a file of size say 500mb. Please let me know the solution.
Thanks,
Hi,
very compliments for your tuttorial, it's very clarity.
I have another question, now.
The command "sendkeys" send data to the active application
in this moment.
I need to send data (key) to an aplication that is just
running, but in background, in minimize state.
It's possible to make this?
Thanks in advantage.
DaxDax
I would really appreciate any help,
Thank you
Is there anyway of using information gathered from drive/dir/file list boxes in the path when using Shell? I have tried but I think I have the wrong syntax even though VB6 does not complain.
I have the same situation and would appreciate your sharing any answer.
Did you check this site?
http://www.planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=6071&lngWId=1
Explorer File(s) (.html or htm) In New Window(new instance of IE for
every file) every time I clicked on the Different file.
hi!! i make a program with .net, in this program use the command shell for execute an aplication in visual basic 6 but i can not open de file "exe" why i need pass by parameters for opening the file.
Somebody can help me with my doubt.
!--removed tag-->I have been trying to get this to work with acrobat. Using the acroRD32 string appears to work, but only on the 2nd attempt. It's odd and possibly something to do with our work security/firewall settings stopping it working. I linked this code with a TOOLSTRIPMENUITEM and on the second time it worked. I have no idea why and if anybody could shed any light I would appreciate it. But, as a temporary fix I have called the function twice.
!--removed tag-->