views:

7352

answers:

5

Hi, 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.

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.

A: 

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

You would also want to check for EOF before using each of these Line Inputs
Patrick McDonald
-1. This fails if the file has less than two lines
Tomalak
It works somewhat, except, I've got this loop at the bottom which goes through the entire text file, looking for each line. And since we not put the the first two lines in a dummy string, the same thing happens at next loop. Meaning, it skips every second line in the text file. Which it should not do. I actually need to have the lines get inserted into an array, which I can use later on.
Kenny Bones
+1  A: 
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
Tomalak
This seems to work pretty well, thanx! :)But what if I want to use the first two lines for anything else?Say, I want to store the first lines in a string, and the second line in another string. Then, store the rest of the lines in an array?
Kenny Bones
If LineNum = 1 Then ... ElseIf LineNum = 2 Then ... ElseIf LineNum > 2 Then ... End If
Tomalak
+1  A: 

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
Remou
+2  A: 

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:

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
Mike Woodhouse
@Mike ... reread you're answer. Solid. Answered the question and showed a better way of doing it. Noice work.
Mark Nold
A: 
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
Sjuul Janssen