Get Free File Number Method

The Get Free File Number method returns the lowest unused file number. It does not include arguments. You can use it if you must supply a file number and must make sure that this file number is not already in use. You can use the return value in a subsequent Open statement.

Format

FreeFile

The following example opens a file and assigns to it the next file number that is available:

Sub Button_Click
   Dim filenumber As Integer
   Dim filename As String
   filenumber = FreeFile
   filename = "d:\temp\trace.txt"
   On Error Resume Next
   Open filename For Input As filenumber
   If Err <> 0 then
      Exit Sub
   End If
   Close #filenumber
End Sub

Open File Method