Hi all,

In my form, i have a button. If i press button, 1 file ( temp.txt or temp.bmp or temp.wmv ... ) will be opened by using default window program. ( I'm using vb 6 ).

How could i do to solve that problem?

10 Years
Discussion Span
Last Post by Sentax

You can use ShellExecute to do this and also pass other options to the receiving application. View below.

'Put this at the top of your form or module

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

'Magical function
Public Function OpenLocation(ByVal WhichFilePath As String, Optional sParams As String = "", Optional sStartIn As String = vbNullString, Optional lngOpenMode As Long = 1) As Long
    OpenLocation = ShellExecute(0, "Open", WhichFilePath, sParams, sStartIn, lngOpenMode)
End Function

'Your button click
Private Sub Command1_Click()

If OpenLocation("notepad.exe", "c:\myfile.txt", "", 1) < 32 Then
    'Failed to open
End If

End Sub

Your not just limited to applications on your computer. You can send this function a slew of things and if it is valid Windows will handle it, else you will get an error back which is trapped in your calling IF statement.


This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.