This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
Enables input/output (I/O) to a file.
OpenpathnameFormode [Accessaccess ] [lock ]As [# ]filenumber [Len =reclength ]
TheOpen statement syntax has these parts:
| Part | Description |
|---|---|
| pathname | Required.String expression that specifies a file name; may include directory or folder, and drive. |
| mode | Required.Keyword specifying the file mode:Append,Binary,Input,Output, orRandom. If unspecified, the file is opened forRandom access. |
| access | Optional. Keyword specifying the operations permitted on the open file:Read,Write, orRead Write. |
| lock | Optional. Keyword specifying the operations restricted on the open file by other processes:Shared,Lock Read,Lock Write, andLock Read Write. |
| filenumber | Required. A validfile number in the range 1 to 511, inclusive. Use theFreeFile function to obtain the next available file number. |
| reclength | Optional. Number less than or equal to 32,767 (bytes). For files opened for random access, this value is the record length. For sequential files, this value is the number of characters buffered. |
You must open a file before any I/O operation can be performed on it.Open allocates a buffer for I/O to the file and determines the mode of access to use with the buffer.
If the file specified bypathname doesn't exist, it is created when a file is opened forAppend,Binary,Output, orRandom modes.
If the file is already opened by another process, and the specified type of access is not allowed, theOpen operation fails and an error occurs.
TheLen clause is ignored ifmode isBinary.
Important
InBinary,Input, andRandom modes, you can open a file by using a different file number without first closing the file. InAppend andOutput modes, you must close a file before opening it with a different file number.
This example illustrates various uses of theOpen statement to enable input and output to a file.
The following code opens the file in sequential-input mode.
Open "TESTFILE" For Input As #1 ' Close before reopening in another mode. Close #1This example opens the file in Binary mode for writing operations only.
Open "TESTFILE" For Binary Access Write As #1 ' Close before reopening in another mode. Close #1The following example opens the file in Random mode. The file contains records of the user-defined type.
Type Record ' Define user-defined type. ID As Integer Name As String * 20 End Type Dim MyRecord As Record ' Declare variable. Open "TESTFILE" For Random As #1 Len = Len(MyRecord) ' Close before reopening in another mode. Close #1This code example opens the file for sequential output; any process can read or write to the file.
Open "TESTFILE" For Output Shared As #1 ' Close before reopening in another mode. Close #1This code example opens the file in Binary mode for reading; other processes can't read the file.
Open "TESTFILE" For Binary Access Read Lock Read As #1Have questions or feedback about Office VBA or this documentation? Please seeOffice VBA support and feedback for guidance about the ways you can receive support and provide feedback.
Was this page helpful?
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?
Was this page helpful?
Want to try using Ask Learn to clarify or guide you through this topic?