Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 199,384 times

Contents

Related Categories

Shell and ShellExecute function - Running another application

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

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments