The new vbAccelerator Site - more VB and .NET Code and Controls

Start a document based on its filename

Author:

Steve McMahon(steve@vbaccelerator.com)

Keywords:

API,Shell

Updated:

01/08/98

Other Tips
All Tips
By Date
By Subject


API (33)
Bit
Manipulation (3)

Clipboard (3)
Combo
Box (5)

Desktop (3)
GDI (13)
Graphics (13)
Internet (2)
Interprocess
Comms (3)

Keyboard (2)
Mouse (1)
Shell (1)
Sprites (1)
Subclassing (3)
Text
Box (2)

Windows (11)
Windows
Controls (10)



Submit


Starting any document file based on its file name only is very simple in Windows 9x and Windows NT using the ShellExecute function. This tip shows how simple it is - you only really need one declare and one line of code!

Start a new project in VB. Add a Command button to the project's form, then add the following code:

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

Private Sub Command1_Click()
Dim lR As Long
Dim sFile As String
Dim iFile As Integer

   
' Create a text file to test:
    sFile = App.Path & "\SHELLTST.TXT"
    On Error Resume Next
    Kill sFile
    On Error GoTo 0
    iFile = FreeFile
    Open sFile For Binary Access Write As #iFile
    Put #iFile, , "This is a text file used for testing the ShellExecute API function."
    Close #iFile

   
' Start the text file using its filename. Windows will
    ' check what exe is associated with .TXT files (by default
    ' this is Notepad) and start the application with the file
    ' opened:
    lR = ShellExecute(Me.hWnd, "Open", sFile, "", "", vbNormalFocus)
    If (lR < 0) Or (lR > 32) Then
       
' success
    Else
        MsgBox "Failed to start '" & sFile & "'", vbInformation
    End If
End Sub


When you click the command button, the app will create a small text file in the project's path, and then open it with the default application (normally Notepad).


 

Related Tips and Articles:

None.

 
 

About  Contribute  Send Feedback  Privacy

Copyright © 1998-1999, Steve McMahon ( steve@vbaccelerator.com). All Rights Reserved.
Last updated: 01/08/98

g="0" cellspacing="0" width="100%">  

About  Contribute  Send Feedback  Privacy

Copyright © 1998-1999, Steve McMahon ( steve@vbaccelerator.com). All Rights Reserved.
Last updated: 01/08/98