Shell and ShellExecute function

Opening a file

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:[email protected]", vbNullString, vbNullString, vbNormalFocus

or to open a URL:

ShellExecute 0&, vbNullString, "http://www.vbweb.co.uk", vbNullString, vbNullString, vbNormalFocus

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.

“There are only two kinds of languages: the ones people complain about and the ones nobody uses” - Bjarne Stroustrup