Open Default App for Any File

By | 2009-11-07

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

Const SW_SHOWMAXIMIZED = 3
Const SW_SHOWMINIMIZED = 2
Const SW_SHOWDEFAULT = 10
Const SW_SHOWMINNOACTIVE = 7
Const SW_SHOWNORMAL = 1

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.

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

lpOperation
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.

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

lpParameters
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.

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

nShowCmd
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!

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.