- 7
- 0
- 约1.51万字
- 约 22页
- 2017-06-05 发布于河南
- 举报
VBA 读写文件(国外英文资料)
VBA 读写文件
Process text file
1, the Open statement
The syntax: Open pathname For mode, [c] As [[]]
The access, lock, and reclength are optional parameters, typically not.
Mode specifies how to open the file. There are five kinds of:
Input: open in the Input mode, read mode.
Output: open in the Output mode, the write mode.
Append: open by Append, adding content to the end of the file.
Binary: open in Binary mode.
Random: open in a Random way and open the file in Random if not specified.
The filenumber is a valid file number between 1 and 511. You can specify that you can also use the FreeFile function to get the next available file number.
Note: if the file specified by the pathname does not exist, this file can be created when the file is opened using Append, Binary, Output, or Random.
Example:
Open F: \ test.txt For Input As # 1 Open in the Input mode
The Open F: \ test.xls For Binary As # 1 is opened in Binary mode
2, Close the statement
Syntax: Close [filenumberlist]
The filenumberlist parameter is one or more file number, and if the filenumberlist is omitted, all the active files opened by the Open statement are closed.
Note: after you open the file, you must close the file after using it.
Example:
Dim I, FileName
For I = 1 To 3
FileName = TEST I creates the file name.
Open FileName For Output As # I Open the file.
Print # I, This is a test. writes the string to the file.
Next I
Close closes all three open files.
3, Reset statement
Grammar: Reset
Functionality: close all disk files Open with Open statements.
Note: the Reset statement closes all the active files opened by the Open statement and writes all the contents of the file buffer to disk.
Example:
Dim FileNumber
For FileNumber = 1 To 5
Open TEST FileNumber For Output As # FileNumber
Write # FileNumber, Hello World to Write the data to the file.
Next FileNumber
Reset close the file and write the data in the buffer to disk.
4, FreeFile function
Syntax: FreeFile [(rangenumber)]
The parameter rangenumber speci
原创力文档

文档评论(0)