• Viewing a text file (vb6)

    Author
    Topic
    #375681

    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

    Viewing 4 reply threads
    Author
    Replies
    • #612344

      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

    • #612354

      (Edited by HansV on 29-Aug-02 13:19. replaced reference to Access window handle.)

      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:


      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

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


      Dim lngResult As Long

      lngResult = ShellExecute(Me.hWnd, “Open”, _
      “”, 0&, 0&, SW_SHOWNORMAL)
      If lngResult <= 32 Then
      MsgBox "Can't open document.", vbExclamation
      End If

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

    • #612377

      You can also easily read a text file into a multiline textbox:

      Open "MyFile.txt" for Input as #1
      Text1.Text = Input(LOF(1), 1)
      Close #1

      HTH –Sam

    • #612652

      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.

      • #612722

        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

        • #612780

          That is a Windows API call. If you want more explanation, look in http://www.allapi.net[/url%5D, 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.

    • #612819

      Sorry, I didn’t realize that you just wanted to see it in a separate window. Shell will work fine:

      Shell "notepad c:myFile.txt", vbNormalFocus
    Viewing 4 reply threads
    Reply To: Viewing a text file (vb6)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: