Returns the next available file number for opening a file. Use this function to open a file using a file number that is not already in use by a currently open file.
FreeFile
Integer
This function can only be used immediately before an Open statement. FreeFile returns the next available file number, but does not reserve it.
5 Invalid procedure call
67 Too many files
Sub ExampleWorkWithAFile Dim iNumber As Integer Dim sLine As String Dim aFile As String Dim sMsg As String aFile = "C:\Users\ThisUser\data.txt" iNumber = Freefile Open aFile For Output As #iNumber Print #iNumber, "This is a line of text" Print #iNumber, "This is another line of text" Close #iNumber iNumber = Freefile Open aFile For Input As iNumber While Not eof(iNumber) Line Input #iNumber, sLine If sLine <>"" Then sMsg = sMsg & sLine & chr(13) End If Wend Close #iNumber MsgBox sMsg End Sub
Sub ExampleWorkWithAFile Dim iNumber As Integer Dim sLine As String Dim aFile As String Dim sMsg As String aFile = "~/data.txt" iNumber = Freefile Open aFile For Output As #iNumber Print #iNumber, "This is a line of text" Print #iNumber, "This is another line of text" Close #iNumber iNumber = Freefile Open aFile For Input As iNumber While Not eof(iNumber) Line Input #iNumber, sLine If sLine <>"" Then sMsg = sMsg & sLine & chr(13) End If Wend Close #iNumber MsgBox sMsg End Sub