Results 1 to 8 of 8
  1. #1
    2 Star Lounger
    Join Date
    Jul 2001
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Viewing a text file (vb6)

    Hello

    I write stuff to a text file...easy enough, but how do I open it so the user can see it using code? I tried shell but I have a feeling it only works with exe files??????

    thanks

  2. #2
    2 Star Lounger
    Join Date
    Nov 2001
    Location
    London, UK, England
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Viewing a text file (vb6)

    Classic way to input and manipulate a text file....

    Public Sub OpenTextFile(sFileName As String)

    Dim sWorkString As String

    '// Open the selected file
    Open sFileName For Input As #1

    '// Check that the file contains at least one record
    If EOF(1) = True Then
    MsgBox "The selected file is empty." & Chr$(13) & "Please return and select another file", 16, "Data Import"
    Exit Sub
    End If

    Do While Not EOF(1)

    '// Read the first record and check its format
    Line Input #1, sWorkString

    '// Now you can get each line from the text file and store it in the sWorkString variable.
    '// Add code to do what you want with the data

    Loop

    Close #1

    End Sub

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Viewing a text file (vb6)

    <P ID="edit" class=small>(Edited by HansV on 29-Aug-02 13:19. replaced reference to Access window handle.)</P>If you want to view the file, you can place a RichTextBox control on a form, and use the LoadFile method of this control. The RichTextBox is not on the Toolbox by defalt. Right click the Toolbox and select Components... In the dialog that appears, check Microsoft Rich Textbox Control 6.0 and click OK.

    Another way is to use the WinAPI function ShellExecute. This opens a file in its own application (*.doc in Word, *.txt in NotePad, *.xls in Excel etc.) Put the following declarations in a standard module:

    <img src=/w3timages/blueline.gif width=33% height=2>
    Public 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

    Public Const SW_SHOWNORMAL = 1
    Public Const SW_SHOWMAXIMIZED As Long = 3
    <img src=/w3timages/blueline.gif width=33% height=2>

    Use code like this to open a document in its own application:

    <img src=/w3timages/blueline.gif width=33% height=2>
    Dim lngResult As Long

    lngResult = ShellExecute(Me.hWnd, "Open", _
    "<path-and-filename>", 0&, 0&, SW_SHOWNORMAL)
    If lngResult <= 32 Then
    MsgBox "Can't open document.", vbExclamation
    End If
    <img src=/w3timages/blueline.gif width=33% height=2>

    (replace <path-and-filename> by the full path and filename of the file you want to display)

  4. #4
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Viewing a text file (vb6)

    You can also easily read a text file into a multiline textbox:
    <pre>Open "MyFile.txt" for Input as #1
    Text1.Text = Input(LOF(1), 1)
    Close #1</pre>

    HTH --Sam
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Viewing a text file (vb6)

    Shell (or the Windows Script Host Shell) will run the associated application. Usually this is Notepad, but if you want to be sure, you can shell a complete command string.

  6. #6
    2 Star Lounger
    Join Date
    Jul 2001
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Viewing a text file (vb6)

    In regards to Hans response. That is exactly what I wanted to do, unfortunatly I just found my self using cot and paste with very little understanding(ummmmm...none to be exact) of what was going on, could you perhaps put the code in lamens terms. I don't see half the byval variables even being used and what is the deal with the whole dim function part. It seems to be way above my head that's all.

    Thanks

  7. #7
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Viewing a text file (vb6)

    That is a Windows API call. If you want more explanation, look in http://www.allapi.net, which offers examples and some information on each of the API calls. Since there are nearly 2000 of them now, people don't keep the explanations in their heads.
    Charlotte

  8. #8
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Viewing a text file (vb6)

    Sorry, I didn't realize that you just wanted to see it in a separate window. Shell will work fine:
    <pre>Shell "notepad c:myFile.txt", vbNormalFocus</pre>

    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •