Results 1 to 4 of 4

Can't open TXT file in VBA

Threaded View

  1. #1
    Registered User
    Join Date
    12-08-2008
    Location
    Sacramento, CA US
    MS-Off Ver
    2007
    Posts
    17

    Can't open TXT file in VBA

    Hello. I need some help. Hopefully it's something simple.

    I am trying to open a txt file, but I get an error message that says

    "Run-time error '52':
    Bad file name or number

    The file name is correct. I tried two ways to pass the name -- as a variable and from Application.GetOpenFilename("Text Files (*.txt), *.txt")

    The line where VBA complains is "Open fileToOpen For Input As #InFile"

    Function ReadFile()
    Dim InFile As Integer, fileToOpen As String, strLine As String
    
    fileToOpen = Application.GetOpenFilename("Text Files (*.txt), *.txt")
    
    Open fileToOpen For Input As #InFile '<-- Error happens here
    
    Do While Not EOF(InFile)
        Line Input #InFile, strLine
        Debug.Print strLine
    Loop
    
    Close #InFile
    End Function
    Any help is much appreciated.

    Thank you,

    Angel
    Last edited by verdugan; 12-23-2008 at 08:58 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1