OPEN(pathname For mode [Access access] [lock] As [#] filenumber [Len = reclength]) |
Opens a text file or CSV file. |
pathname | The file name which can include folder and even drive |
mode | The type of access: Input - Sequential Access that allows read only Output - Sequential Access that allows read and write Append - Sequential Access that allows read and write to the end of the file Binary Random |
access | (Optional) The keyword to specify the type of operation: Read Write Read Write |
lock | (Optional) The keyword to specify the type of lock: Shared Lock Read Lock Write Lock Read Write |
filenumber | The number of the file (Integer). |
reclength | (Optional) The length of a record for Random access and the buffer size for Sequential. |
REMARKS |
* If the file is already open then an error will occur. * You can use the CLOSE statement to close a text file. * You can use the EOF function to indicate when the end of a file has been reached. * You can use the FREEFILE function to return the next free file number. * You can use the LINE INPUT statement to read a single line. * You can use the PRINT statement to write display formatted data to a sequential file. * You can use the WRITE statement to write data to a sequential file. * This statement is not available in Access. * For the Microsoft documentation refer to learn.microsoft.com |
Open "C:\Temp\MyText.txt" For Output As #1
Close #1
Open "C:\Temp\MyText.txt" For Output As #1
VBA.Print #1, "one,two,three"
VBA.Write #1, "four,five,six"
VBA.Close #1
Open "C:\Temp\MyText.txt" For Input As #1
Debug.Print LOF(1)
Do Until VBA.EOF(1)
Dim LineFromFile As String
Line Input #1, LineFromFile
Dim VarName1, VarName2 As Variant
Input #1, VarName1, VarName2
Debug.Print LineFromFile
Debug.Print VarName1, VarName2
Loop
Close #1
Open "C:\Temp\MyText.txt" For Binary As #1
VBA.Close #1
Open "C:\Temp\MyText.txt" For Binary Access Read Lock Read As #1
VBA.Close #1
Open "C:\Temp\MyText.txt" For Append As #1
VBA.Close #1
Open "C:\Temp\MyText.txt" For Random Shared As #1 Len = 15
VBA.Close #1
© 2023 Better Solutions Limited. All Rights Reserved. © 2023 Better Solutions Limited Top