Open 语句Open statement

启用对文件的输入/输出 (I/O)。Enables input/output (I/O) to a file.

语法Syntax

mode [ Access Access ] [ lock ]打开 pathname ,作为[ # ] filenumber [ Len = reclength ]Open pathname For mode [ Access access ] [ lock ] As [ # ] filenumber [ Len = reclength ]


Open 语句的语法包含以下部分:The Open statement syntax has these parts:

PartPart 说明Description
pathnamepathname 必填。Required. 指定文件名的字符串表达式,可包括目录或文件夹和驱动器。String expression that specifies a file name; may include directory or folder, and drive.
modemode 必填。Required. 指定文件模式的关键字AppendBinaryInputOutputRandomKeyword specifying the file mode: Append, Binary, Input, Output, or Random. 如果未指定,则以 Random 访问模式打开文件。If unspecified, the file is opened for Random access.
访问access 可选。Optional. 指定可对打开的文件执行的操作的关键字:ReadWriteRead WriteKeyword specifying the operations permitted on the open file: Read, Write, or Read Write.
lock 可选。Optional. 指定由其他进程限制在打开的文件上的操作的关键字:共享锁定读取锁定写入锁定读写Keyword specifying the operations restricted on the open file by other processes: Shared, Lock Read, Lock Write, and Lock Read Write.
filenumberfilenumber 必填。Required. 一个有效文件号,范围为 1 到 511(含 1 和 511)。A valid file number in the range 1 to 511, inclusive. 使用**FreeFile** 函数可获取下一个可用的文件编号。Use the FreeFile function to obtain the next available file number.
reclengthreclength 可选。Optional. 小于或等于 32,767(字节)的数。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.

备注Remarks

只有打开文件后才能对其执行任何 I/O 操作。You must open a file before any I/O operation can be performed on it. Open 分配一个缓冲区以对文件执行 I/O 操作,同时确定用于此缓冲区的访问模式。Open allocates a buffer for I/O to the file and determines the mode of access to use with the buffer.

如果 pathname 指定的文件不存在,那么在以 AppendBinaryOutputRandom 模式打开文件时,即会创建它。If the file specified by pathname doesn't exist, it is created when a file is opened for Append, Binary, Output, or Random modes.

如果文件已由另一个进程打开,并且不允许指定的访问类型,则打开操作将失败,并将发生错误。If the file is already opened by another process, and the specified type of access is not allowed, the Open operation fails and an error occurs.

如果 modeBinary,则忽略 Len 子句。The Len clause is ignored if mode is Binary.

重要

二进制输入随机模式中,您可以使用不同的文件号打开文件,而无需先关闭文件。In Binary, Input, and Random modes, you can open a file by using a different file number without first closing the file. AppendOutput 模式中,必须先关闭该文件,然后才能用不同文件号打开它。In Append and Output modes, you must close a file before opening it with a different file number.

示例Example

此示例说明各种使用 Open 语句启用对文件的输入和输出的情况。This example illustrates various uses of the Open 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 #1 


此示例在二进制 (Binary) 模式下打开文件,仅供写操作。This 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 #1 


下面的示例在随机 (Random) 模式下打开文件。The 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 #1 


此代码示例打开文件以进行顺序输出;任何进程都可以读取或写入文件。This 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 #1 

此代码示例以二进制模式打开文件以供阅读;其他进程无法读取该文件。This 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 #1 

另请参阅See also

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.