Consulting

Results 1 to 13 of 13

Thread: Reading a Text File into an Intermediate Window

  1. #1

    Reading a Text File into an Intermediate Window

    How do I read in a text file into VBA and print it out in the Intermediate Window?

  2. #2
    VBAX Expert Leith Ross's Avatar
    Joined
    Oct 2012
    Location
    San Francisco, California
    Posts
    552
    Location
    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
    Sincerely,
    Leith Ross

    "1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG"

  3. #3
    VBAX Expert Leith Ross's Avatar
    Joined
    Oct 2012
    Location
    San Francisco, California
    Posts
    552
    Location
    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
    Sincerely,
    Leith Ross

    "1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG"

  4. #4
    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.

  5. #5
    VBAX Expert Leith Ross's Avatar
    Joined
    Oct 2012
    Location
    San Francisco, California
    Posts
    552
    Location
    Hello AK,

    Did you try zipping the file before posting it?
    Sincerely,
    Leith Ross

    "1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG"

  6. #6
    opus.zip

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

  7. #7
    VBAX Expert Leith Ross's Avatar
    Joined
    Oct 2012
    Location
    San Francisco, California
    Posts
    552
    Location
    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
    Sincerely,
    Leith Ross

    "1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG"

  8. #8
    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.

  9. #9
    VBAX Expert Leith Ross's Avatar
    Joined
    Oct 2012
    Location
    San Francisco, California
    Posts
    552
    Location
    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.
    Sincerely,
    Leith Ross

    "1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG"

  10. #10
    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.

  11. #11
    MS Excel MVP VBAX Mentor Andy Pope's Avatar
    Joined
    May 2004
    Location
    Essex, England
    Posts
    344
    Location
    There is a limit of ~200 lines in the immediate window before it starts removing top lines.
    Cheers
    Andy

  12. #12
    Ah gotcha. That makes sense. How would I go about limiting it to print out the first 200 lines? Now I'm curious. Thanks!

  13. #13
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    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
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •