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

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

问题描述

我在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编辑器中的Intellisense令人难以忍受btw ..

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.

推荐答案

整个打开<文件路径>对于输入为<一些数字> 事情是所以 20世纪90年代。它也很慢,非常容易出错。

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

在VBA编辑器中,从工具菜单中选择引用,然后查找Microsoft Scripting Runtime(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天全站免登陆