Contents

# FreeFile Function

傳回下一個可用檔案號碼以開啟檔案。使用此函式透過一個未被目前已開啟的檔案所使用的檔案號碼來開啟一個檔案。

### Syntax:

    FreeFile

### Return value:

整型

### Parameters:

僅當此函式直接放在 Open 陳述式之前時才可以被使用。FreeFile 傳回下一個可用的檔案號碼，但不會保留此號碼。

### 錯誤代碼：

5 無效的程序呼叫

67 檔案過多

### Example:

      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

相關主題

Close Statement

Open Statement

Print# Statement