PDA

View Full Version : Reading a Text File into an Intermediate Window



AK_Beaver
05-02-2017, 12:44 PM
How do I read in a text file into VBA and print it out in the Intermediate Window?

Leith Ross
05-02-2017, 01:04 PM
Hello AK_Beaver,

If you mean the Immediate Window then this code will work. Copy it to a VBA Module and change the FileName to the text file you want to view.



Sub ReadTextFile()


Dim FileName As String
Dim TextLine As String

FileName = "C:\Read Me.txt"

Open FileName For Input As #1
While Not EOF(1)
Line Input #1, TextLine
Debug.Print TextLine
Wend
Close #1

End Sub

Leith Ross
05-02-2017, 01:05 PM
Hello AK_Beaver,

If you mean the Immediate Window then this code will work. Copy it to a VBA Module and change the FileName to the text file you want to view.



Sub ReadTextFile()


Dim FileName As String
Dim TextLine As String

FileName = "C:\Read Me.txt"

Open FileName For Input As #1
While Not EOF(1)
Line Input #1, TextLine
Debug.Print TextLine
Wend
Close #1

End Sub

AK_Beaver
05-02-2017, 05:02 PM
Yeah I meant Immediate Window. Thanks for the code, it prints the majority of my text file but leaves off the top quarter. Is their a limit to how much can be printed in the window? I've tried uploading the file but I think it's to large for the site.

Leith Ross
05-02-2017, 05:44 PM
Hello AK,

Did you try zipping the file before posting it?

AK_Beaver
05-02-2017, 07:11 PM
19070

Here's the text file I am trying to read.

Leith Ross
05-03-2017, 10:20 AM
Hello AK,

My testing with your file indicates the Immediate Window is very slow in processing the data. I modified the macro to use a time delay of 25 milliseconds. This work well on my computer. You may need to adjust the speed for your computer (faster or slower). Change FileName to match the file you want to open.



Sub ReadTextFile1()


Dim Delay As Single
Dim FileName As String
Dim LineText As String
Dim Text As String

FileName = "C:\Users\Owner\Documents\AK_Beaver\opus.txt"

Open FileName For Input As #1
While Not EOF(1)
Line Input #1, LineText
Debug.Print LineText
Delay = Timer + 0.0025
While Timer < Delay: DoEvents: Wend
Wend
Close #1

End Sub

AK_Beaver
05-03-2017, 09:19 PM
Hey Leith,

Did the whole thing print for you? I'm still just getting like 3/4's of it printed out. Highest line is:

STATION NAME: zan1 a 4 (ANCHORAGE WAAS 1; Anchorage, Alaska, U.S.A.)

Not sure what to do.

Leith Ross
05-03-2017, 10:03 PM
Hello AK,

It displayed the complete text in the Immediate Window for me with the delay set to 0.025 seconds. Increase the delay to to 0.5 seconds (1/2 second or 500 milliseconds). This should be slow enough to allow the Immediate Window to process the text and display it before the macro process the next line of text.

Not sure why you want to display a complete text file in the Immediate Window. Generally, the window is used to view your code's results while stepping through it and not as a general output device.

AK_Beaver
05-03-2017, 11:07 PM
My professor wants it that why. Don't ask me why haha. Thanks. I'll look into it some more. I appreciate the help. I've got a decent grasp of coding but this stuff is beyond me.

Andy Pope
05-04-2017, 01:24 AM
There is a limit of ~200 lines in the immediate window before it starts removing top lines.

AK_Beaver
05-04-2017, 02:32 AM
Ah gotcha. That makes sense. How would I go about limiting it to print out the first 200 lines? Now I'm curious. Thanks!

mdmackillop
05-04-2017, 02:59 AM
Try

Sub ReadTextFile1()
Dim Delay As Single
Dim FileName As String
Dim LineText As String
Dim Text As String
Dim i As Long

FileName = "C:\Users\Owner\Documents\AK_Beaver\opus.txt"

Open FileName For Input As #1
While Not EOF(1)
i = i + 1
If i = 200 Then GoTo Exits
Line Input #1, LineText
Debug.Print LineText
Delay = Timer + 0.0025
While Timer < Delay: DoEvents: Wend

Wend
Exits:
Close #1
End Sub