Calling a file in text format in vb

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rosewitter
    New Member
    • Jul 2010
    • 3

    #1

    Calling a file in text format in vb

    Hie
    how do you call a text file in vb using textbox and command button
  • Guido Geurs
    Recognized Expert Contributor
    • Oct 2009
    • 767

    #2
    with Commondialog if You want to select a file=

    Code:
    Private Function Open_File()
    Dim STRTEMP As String
       TextBox.Text = ""
       On Error GoTo Cancel_Function
       With CommonDialog
          .CancelError = True
          .Filter = "Text (.txt)|*.txt"
          .InitDir = Environ$("USERPROFILE") & "\My Documents"
          .DialogTitle = "Open a file..."
          .ShowOpen
          On Error GoTo Error_Open_File
          Open .FileName For Input As #1
             Do Until EOF(1)
                Input #1, STRTEMP
                TextBox.Text = TextBox.Text & STRTEMP
             Loop
          Close #1
          Caption = Left(Caption, 26) & ": " & .FileName
       End With
    Exit Function
    Error_Open_File:
       Close #1
       MsgBox "There is an error opening the file"
    Exit Function
    Cancel_Function:
    End Function
    If the file is always the same=

    Code:
    Private Sub Form_Load()
    Dim FileIn As Integer
       FileIn = FreeFile
       On Error GoTo Error_Laden
       Open App.Path & "\document.txt" For Input As #FileIn
          Do While Not EOF(FileIn)
             Input #FileIn, inputdata
             TEXTBOX.text = TEXTBOX.text & inputdata & vbCrLf
          Loop
       Close #FileIn
    Exit Sub
    Error_Laden:
       Close #FileIn
       MsgBox ("Error loading !")
    End Sub

    Comment

    • Brian Deering

      #3
      I'm trying to implement the "with Commondialog if You want to select a file" code in Word 2007 (SP2) and when I execute the code it chokes on 'TextBox.Text = ""' and I get error "424 Object Required." Any suggestions?

      Thank you in advance.

      Comment

      • Guido Geurs
        Recognized Expert Contributor
        • Oct 2009
        • 767

        #4
        I think You have to add a "Common Dialog Control" (see GIF) to Your document (see attachment)
        Attached Files

        Comment

        Working...