Sub OpenTextFileTest
    Const ForReading = 1, ForWriting = 2, ForAppending = 3
    Dim fs, f
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.OpenTextFile("c:\testfile.txt", ForReading)
    ...
    '已到读到文本尾
    '以下是返回
    f.Close
    Set f = fs.OpenTextFile("c:\testfile.txt", ForReading)
    '其实就是先关闭,再打开
End Sub

解决方案 »

  1.   

    Private Sub mnuFileOpen_Click()
        '打开文件并自动匹配
        CommonDialog1.CancelError = True
        On Error GoTo errHander
        CommonDialog1.DialogTitle = "选择文件"
        CommonDialog1.Filter = "文件文件(*.txt)|*.txt"
        CommonDialog1.InitDir = App.Path + "\savefiles"
        CommonDialog1.ShowOpen
        '设置FSO对象
        Dim openfso As New FileSystemObject, openfile As TextStream
        
        '可以统计文本文件行数
        'Static counter As Integer
        'counter = 0
        'Do While Not openfile.AtEndOfLine
         '   openfile.ReadLine
          '  counter = counter + 1
        'Loop
        
        Dim vDoc, vTag
        Dim i As Integer
        Dim j As Integer
        Dim desstring, myarray '定义每行的目的字符串,及数组对象
        Set vDoc = Me.WebBrowser1.Document
        For i = 0 To vDoc.All.Length - 1
            If UCase(vDoc.All(i).tagname) = "INPUT" Then
                Set vTag = vDoc.All(i)
                  If vTag.Type = "text" Or vTag.Type = "textarea" Then
                    Set openfile = openfso.OpenTextFile(CommonDialog1.FileName, ForReading)
                    '自动匹配算法
                    Do While Not openfile.AtEndOfLine
                      
                      desstring = openfile.ReadLine
                      myarray = Split(desstring, "$", -1, vbTextCompare)
                      If vTag.Name = myarray(0) Then
                        vTag.Value = myarray(1)
                      End If 'end if value
                      
                    Loop 'loop the while
                   '退回到文本头
                   
                  End If 'end if vtag.type="text"...
            End If 'end if ucase(....)
        Next i 'next i
        MsgBox "所有信息已提取", vbOKOnly + vbInformation, "成功读取文件"
        Exit Sub
    errHander:
        Exit Sub
        
    End Sub
    这是一个自动配的算法,但是只能配上第一项,不能全配上