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.
我在Microsoft Office Word 2003中有这个宏代码,它读取文本文件的行。每一行都表示一个字符串值,我需要在后面的代码中使用它。
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..
但是,文本文件的前两行包含一些我不需要的东西。如何修改代码,使其跳过前两行?顺便说一句,VBA编辑器中的“智能感知”非常糟糕。
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.
这段代码现在给了我所有的行,我不想要前两行。
5 个解决方案
#1
30
That whole Open <file path> For Input As <some number>
thing is so 1990s. It's also slow and very error-prone.
整个打开的
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:
在VBA编辑器中,从“工具”菜单中选择“引用”,并查找“Microsoft脚本运行时”(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
#2
6
You can use random access.
你可以使用随机访问。
Open "C:\docs\TESTFILE.txt" For Random As #1
Position = 3 ' Define record number.
Get #1, Position, ARecord ' Read record.
Close #1
#3
3
Open sFileName For Input As iFileNum
Dim LineNum As Long
LineNum = 0
Do While Not EOF(iFileNum)
LineNum = LineNum + 1
Line Input #iFileNum, Fields
If LineNum > 2 Then
DoStuffWith(Fields)
End If
Loop
#4
2
May be I am oversimplifying?
我是不是过于简单化了?
Just add the following code:
只需添加以下代码:
Open sFileName For Input as iFileNum
Line Input #iFileNum, dummy1
Line Input #iFileNum, dummy2
........
Sundar
Sundar
#5
1
Dim sFileName As String
Dim iFileNum As Integer
Dim sBuf As String
Dim Fields as String
Dim TempStr 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
''//This part skips the first two lines
if not(EOF(iFileNum)) Then Line Input #iFilenum, TempStr
if not(EOF(iFileNum)) Then Line Input #iFilenum, TempStr
Do While Not EOF(iFileNum)
Line Input #iFileNum, Fields
MsgBox (Fields)
Loop
#1
30
That whole Open <file path> For Input As <some number>
thing is so 1990s. It's also slow and very error-prone.
整个打开的
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:
在VBA编辑器中,从“工具”菜单中选择“引用”,并查找“Microsoft脚本运行时”(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
#2
6
You can use random access.
你可以使用随机访问。
Open "C:\docs\TESTFILE.txt" For Random As #1
Position = 3 ' Define record number.
Get #1, Position, ARecord ' Read record.
Close #1
#3
3
Open sFileName For Input As iFileNum
Dim LineNum As Long
LineNum = 0
Do While Not EOF(iFileNum)
LineNum = LineNum + 1
Line Input #iFileNum, Fields
If LineNum > 2 Then
DoStuffWith(Fields)
End If
Loop
#4
2
May be I am oversimplifying?
我是不是过于简单化了?
Just add the following code:
只需添加以下代码:
Open sFileName For Input as iFileNum
Line Input #iFileNum, dummy1
Line Input #iFileNum, dummy2
........
Sundar
Sundar
#5
1
Dim sFileName As String
Dim iFileNum As Integer
Dim sBuf As String
Dim Fields as String
Dim TempStr 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
''//This part skips the first two lines
if not(EOF(iFileNum)) Then Line Input #iFilenum, TempStr
if not(EOF(iFileNum)) Then Line Input #iFilenum, TempStr
Do While Not EOF(iFileNum)
Line Input #iFileNum, Fields
MsgBox (Fields)
Loop