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

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

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


&nbsp

Related Tips and Articles:

None.

&nbsp
 

About  Contribute  Send Feedback  Privacy

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