OPEN(pathname For mode [Access access] [lock] As [#] filenumber [Len = reclength])

Opens a text file or CSV file.


pathnameThe file name which can include folder and even drive
modeThe 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
filenumberThe 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.
* For the Microsoft documentation refer to docs.microsoft.com

Open "C:\Temp\MyText.txt" For Output As #1 
Close #1

Open "C:\Temp\MyText.txt" For Output As #1
Print #1, "one,two,three"
Write #1, "four,five,six"
Close #1

Open "C:\Temp\MyText.txt" For Input As #1
Debug.Print LOF(1)
Do Until 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
Close #1

Open "C:\Temp\MyText.txt" For Binary Access Read Lock Read As #1
Close #1

Open "C:\Temp\MyText.txt" For Random Shared As #1 Len = 15
Close #1

© 2019 Better Solutions Limited. All Rights Reserved. © 2019 Better Solutions Limited Top