VB读txt文件怎么读?谁有代码,给一段吧

解决方案 »

  1.   

    dim TmpStr as string
    Open c:\aa.txt For Input As #1       '打开文本文件
    Do While Not EOF(1)
        Line Input #1, TmpStr              '读取一行的内容
         msgbox TmpStr
    Loop
    Close #1
      

  2.   

    太多了Public Sub File_ReadAll(sFile As String) '将txt文本全部读入
        Dim fs, f, s
        Set fs = CreateObject("Scripting.FileSystemObject")
        Set f = fs.OpenTextFile(sFile, 1)
            s = f.readall
        f.Close: Set f = Nothing: Set fs = Nothing
        Debug.Print s
    End SubPublic Sub File_ReadLine(sFile As String) ’一行一行读入文本,对单行进行处理
        Dim fs, f, s
        Set fs = CreateObject("Scripting.FileSystemObject")
        
        Set f = fs.OpenTextFile(sFile, 1)
        Do
            s = f.readLine
            '处理过程代码
            '
            '
        Loop Until f.AtEndOfStream
        f.Close: Set f = Nothing: Set fs = Nothing
    End Sub
      

  3.   


    Option Explicit
    Dim strFile As String
    Dim strMemo As String
    Dim intFileNum As Integer
    '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    '过程功能:
    '功能描述:Input方式读入文件
    '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    Private Sub btnInput_Click()
    On Error GoTo errSub
        strMemo = ""
        intFileNum = FreeFile(0)
        Open strFile For Input As #intFileNum
        Do While Not EOF(intFileNum)
            Line Input #intFileNum, strMemo
            txtMemo.Text = txtMemo.Text & strMemo & vbCrLf
        Loop
        Close #intFileNum
        Exit Sub
    errSub:End SubPrivate Sub Form_Load()
        strFile = App.Path & "\111.txt"
        
    End Sub
      

  4.   

    楼主应该掌握一种自己解决问题的方法。现在网络这么发达,楼主又能上网,那么这类问题就应该很容易自己解决。在www.baidu.com或www.google.com.hk的搜索框中输入vb txt。答案就会出来了。像此类的问题,都可以通过这种方法解决。(我的经历,仅供参考)