Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleOpen a text file in the system's default text editor
DescriptionThis example shows how to open a text file in the system's default text editor in Visual Basic 6.
Keywordseditor, default editor, system editor, text file
CategoriesAPI, Files and Directories, Windows
 
The FindAssociatedProgram function returns the name of the program associated with a file extension. It uses GetTempFile to get the name of a temporary file with the extension, creates the file, uses the FindExecutable API function to find the program associated with it, and then deletes the temporary file.

Subroutine GetTempFile makes a file in the system's TEMP directory. For extension "ext," it loops through files 1.ext, 2.ext, and so forth until it finds a file name that doesn't yet exist.

 
' Return the path to the program associated with this
' extension.
Private Function FindAssociatedProgram(ByVal extension As _
    String) As String
Dim temp_title As String
Dim temp_path As String
Dim fnum As Integer
Dim result As String
Dim pos As Integer

    ' Get a temporary file name with this extension.
    GetTempFile extension, temp_path, temp_title

    ' Make the file.
    fnum = FreeFile
    Open temp_path & temp_title For Output As fnum
    Close fnum

    ' Get the associated executable.
    result = Space$(1024)
    FindExecutable temp_title, temp_path, result
    pos = InStr(result, Chr$(0))
    FindAssociatedProgram = Left$(result, pos - 1)

    ' Delete the temporary file.
    Kill temp_path & temp_title
End Function

' Return a temporary file name.
Private Sub GetTempFile(ByVal extension As String, ByRef _
    temp_path As String, ByRef temp_title As String)
Dim i As Integer

    If Left$(extension, 1) <> "." Then extension = "." & _
        extension

    temp_path = Environ("TEMP")
    If Right$(temp_path, 1) <> "\" Then temp_path = _
        temp_path & "\"

    i = 0
    Do
        temp_title = "tmp" & Format$(i) & extension
        If Len(Dir$(temp_path & temp_title)) = 0 Then Exit _
            Do
        i = i + 1
    Loop
End Sub
 
When you click the Open button, the program uses FindAssociatedProgram to find the program associated with .txt files. Then it uses the ShellExecute API function to "open" that program, passing it the name of the text file as a parameter.
 
Private Sub cmdOpen_Click()
Dim txt_program As String
Dim target_file As String

    ' Get the program associated with .txt files.
    txt_program = FindAssociatedProgram(".txt")
    lblResult.Caption = txt_program

    ' Add quotes to the file name.
    target_file = """" & txtFile.Text & """"

    ' Use that program to open the file.
    ShellExecute Me.hwnd, "open", txt_program, target_file, _
        vbNullString, SW_SHOWMAXIMIZED
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated