从文本文件中读取行但跳过前两行 [英] Read lines from a text file but skip the first two lines

查看:32
本文介绍了从文本文件中读取行但跳过前两行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've got this macro code in Microsoft Office Word 2003 which reads the lines of a text file. The lines each represent a string value that I need to use later in the code.

然而,文本文件的前两行包含一些我不需要的东西.如何修改代码使其跳过前两行?Word 中 VBA 编辑器中的智能感知"很糟糕,顺便说一句..

However, the first two lines of the text file contains some stuff that I don't need. How can I modify the code so that it skips the two first lines? The "Intellisense" within the VBA editor in Word sucks hard btw..

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

Anyway, the code looks something like this

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)

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

And this code currently gives me all of the lines, and I don't want the first two.

推荐答案

整个 Open 对于 Input As 来说,so 是 1990 年代.它也很慢而且很容易出错.

That whole Open <file path> For Input As <some number> thing is so 1990s. It's also slow and very error-prone.

在您的 VBA 编辑器中,从工具"菜单中选择引用"并查找Microsoft 脚本运行时"(scrrun.dll),它几乎可以在任何 XP 或 Vista 计算机上使用.它就在那里,选择它.现在您可以使用(至少对我而言)更强大的解决方案:

In your VBA editor, Select References from the Tools menu and look for "Microsoft Scripting Runtime" (scrrun.dll) which should be available on pretty much any XP or Vista machine. It it's there, select it. Now you have access to a (to me at least) rather more robust solution:

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

这篇关于从文本文件中读取行但跳过前两行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆