VB6:Open Default App for Any File

Posted On 2009-11-07 by dwirch
Keywords: VB6 Visual Basic 6 Visual Studio Open File API
Tags: VB6 Miscellaneous Tutorial Tip 
Views: 6138

Handling links (or other file types) in your VB6 application can be a bit of a pain in the neck. The user may not use what you use to view text files. How to handle it?

Let Windows handle it. By utilizing the ShellExecute API, we can have Windows open any file type with its associated application. Word .doc files will open with Word, or Wordpad. Notepad .txt documents might open with Notepad or some other viewer.

Even http: links will open with the users default browser, whether it is Internet Explorer, Firefox, Chrome, Opera, or whatever.

Some of you may be a bit spooked about using API calls. Don't be. There is nothing magical or mystical about it. You *can* shoot yourself in the foot. But if you are careful, you can do some pretty amazing things with just a few bits of code.

This "magical" bit of code is built to be dropped into a module. There are three sections to it, which I'll explain now.

In the first section, we'll define some constants. These constants define how Windows will display the shelled program on the screen: Maximized with focus, Minimized with focus, Default, Minimized without focus, and Normal.

Before anything, Option Explicit is used to ensure all variables get defined before use. I highly recommend getting into this habit!
Option Explicit


Next, we'll actually define our API call. The particular function that will be used is in shell32.dll, and is known internally as "ShellExecuteA".

The reference to the .dll and ShellExecuteA is followed by the list of parameters that will be used to modify the behavior of the function. Here is a list of the parameters, with an explanation of each.

Identifies the parent window. This window receives any message boxes an application produces (for example, for error reporting).

Points to a null-terminated string specifying the operation to perform. This string can be "open" or "print." If this parameter is NULL, "open" is the default value.

Points to a null-terminated string specifying the file to open.

Points to a null-terminated string specifying parameters passed to the application when the lpszFile parameter specifies an executable file. If lpszFile points to a string specifying a document file, this parameter is NULL.

Points to a null-terminated string specifying the default directory.

Specifies whether the application window is to be shown when the application is opened.

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

For the last piece of the module, here is the function that processes the passed arguments to the API. This is the function that is called from the main program.
Private Function OpenLocation(Target As String, WindowState As Long) As Boolean

Dim lHWnd As Long
Dim lAns As Long

lAns = ShellExecute(lHWnd, "open", Target, vbNullString, _
vbNullString, WindowState)
OpenLocation = (lAns > 32)

End Function

And how do we call the function? From the main program, you'll want to define a boolean variable, which will store a true/false value that gives us the success or failure status of the call.

On the next line, you call the function, passing the appropriate parameters:
Dim ret as Boolean
ret = OpenLocation("http://www.fortypoundhead.com", SW_SHOWNORMAL)

This will open the site in the default browser, in the normal display mode, with focus.

By using this API method for opening files and URLs, you can do so with one simple line of code, calling a function from your main program. I hope you've found this useful!

About the Author

dwirch has posted a total of 188 articles.

Comments On This Post

By: AnonymousCoward
Date: 2014-08-19

tastes link a mint, works like a miricale

By: dwirch
Date: 2014-08-19

Glad you found it useful!

Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.

Your IP address is:

Before you can post, you need to prove you are human. If you log in, this test goes away.

Code Links