Launch a File Using Its Associated Application - February 2000

I recently had a requirement to provide an 'Explorer' list view that contained a list of files linked to an application. Then I needed to let the user launch the file using its associated application. I poised a question on the 'news.devx.com' news group asking how to do this and got several good responses. I have since seen this requested several times in news groups so I decided to make it the tip-of-the-month.

The Demo Program

I wrote a VB6 demo program to show how the function works. The demo program:

I included several 'sample' files. When you double-click the sample file name or click button 'Shell Execute Using the Selected File's Association' the associated application is launched. For example, when clicked on 'Sample.txt' 'Notepad' is launched:

Use 'Open With' if No Association Exists

If there is no associated application then the 'Open With' dialog is shown, for example:

Add Files to the List Box

Use the 'Browse for Files to Add to the List' button to open a 'browse' window, for example:


Uses CDialog.cls from Francesco Balena

Include in Your Code

Include the following class modules in your code to use the functions:

clsShellExecuteUsingFileName.cls Performs the Shell Execute and Open With functions

Sample Call to clsShellEX 

Private Function DoLaunch(frm As Form) As Boolean
   Dim obj As New clsShellEX 'get ready to launch
   With obj 'set some values
      .sFileName = List1.List(List1.ListIndex) 'file to launch
      .lHwnd = frm.hWnd 'my handle
      If .ShellExecuteUsingFileName() Then 'do it
         DoLaunch = True 'success
      Else
         ' Add error handling...
         MsgBox .sErrorMessage, vbExclamation, "Error code: " & .lRtn
      End If
   End With
End Function

clsShellEX Properties and Method

sFileName Fully qualified name of file to open.
lHwnd Caller's Handle, frm.hWnd.
lRtn Return Code if any error.
sErrorMessage Returned error message if any error.
ShellExecuteUsingFileName Launch sFileName, return True if successful. 

The Demo Uses modConnectToWebSite

The demo program uses modConnectToWebSite.bas to display this page. For details on the use of that module click here.

Microsoft Similar Topic

2001/10/22 I recently found the following similar topic on Microsoft's web site: HOWTO: Use ShellExecute to Launch Associated File (32-bit)

Download VB Code

Click here to download the VB 6 Source Code [17k]. 

Previous Tip   Tip Index   Next Tip

Home

Updated 2001/10/22