从文本文件中读取行但跳过前两行

Ken*_*nes 15 excel vba ms-word excel-vba text-files

我在Microsoft Office Word 2003中有这个宏代码,它读取文本文件的行.每行代表一个字符串值,我需要稍后在代码中使用.

但是,文本文件的前两行包含一些我不需要的东西.如何修改代码以便跳过前两行?Word中的VBA编辑器中的"Intellisense"糟透了.

无论如何,代码看起来像这样

Dim sFileName As String
Dim iFileNum As Integer
Dim sBuf As String
Dim Fields as String

sFileName = "c:\fields.ini"
''//Does the file exist?
If Len(Dir$(sFileName)) = 0 Then
    MsgBox ("Cannot find fields.ini")
End If

iFileNum = FreeFile()
Open sFileName For Input As iFileNum
Do While Not EOF(iFileNum)
    Line Input #iFileNum, Fields

    MsgBox (Fields)
Run Code Online (Sandbox Code Playgroud)

这个代码目前给了我所有的代码,我不想要前两个代码.

Mik*_*use 30

整个Open <file path> For Input As <some number>事情都是如此.它也很慢,非常容易出错.

在您的VBA编辑器中,从"工具"菜单中选择"引用",然后查找"Microsoft Scripting Runtime"(scrrun.dll),它几乎可以在任何XP或Vista计算机上使用.它就在那里,选择它.现在您可以访问(至少对我来说)更强大的解决方案:

With New Scripting.FileSystemObject
    With .OpenTextFile(sFilename, ForReading)

        If Not .AtEndOfStream Then .SkipLine
        If Not .AtEndOfStream Then .SkipLine

        Do Until .AtEndOfStream
            DoSomethingImportantTo .ReadLine
        Loop

    End With
End With
Run Code Online (Sandbox Code Playgroud)


Fio*_*ala 6

您可以使用随机访问.

Open "C:\docs\TESTFILE.txt" For Random As #1 

    Position = 3    ' Define record number.
    Get #1, Position, ARecord    ' Read record.

Close #1
Run Code Online (Sandbox Code Playgroud)