Shell and ShellExecute function

Page 1 of 2
  1. Running another application
  2. Opening a file

Running another application

You can run another application by using the Shell statement. 

'// this code calls c: est.exe
Shell "c:\test.exe", vbNormalFocus

You can also call any program in the windows directory, such as regsvr32, notepad, and explorer

So, 

Shell "notepad", vbNormalFocus

Runs notepad, and

Shell "explorer", vbNormalFocus

runs explorer. You can also pass any command parameters you need. For example:

Shell "notepad C:\test.txt", vbNormalFocus

runs notepad, and tells it to open C:\test.txt. Note that if the path you pass contains spaces, you need to surround it by quotes:

Shell "notepad ""C:\dir with spaces\test.txt""", vbNormalFocus

which is actually the equivalent of passing this:

notepad " C:\dir with spaces\test.txt"

The second 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 shell function also returns a TaskID (double), that you can use with the AppActivate statement:

Private dblNotePadID As Double
'// runs word
Private Sub cmdRunNotePad_Click()
    dblNotePadID = Shell("notepad", vbNormalFocus")
End Sub
'// activates word at a later stage
Private Sub cmdActivateNotePad_Click()
    AppActivate dblNotePadID
End Sub

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Engineers are all basically high-functioning autistics who have no idea how normal people do stuff.” - Cory Doctorow