Read and Write Files
There are a number of different ways to read and write files in Visual Basic, and which you choose depends on what you are trying to do, as described in Table 3-13.
Table 3-13. File-access techniques in Excel Visual Basic
|
Technique |
Use to |
Look here |
|---|---|---|
|
Intrinsic functions |
Read or write simple datafiles |
This section |
|
|
Create files, folders, and control file attributes | |
|
|
Create, open, and save Excel workbook files; import datafiles into workbooks | |
|
|
Import or export XML datafiles from a workbook |
In short, you shouldn’t assume the Visual Basic intrinsic functions are the best way to read and write files in all situations. Actually, I prefer the FileSystemObject for most general file-access tasks, but it’s important to be thorough, so I’ll cover the intrinsic file-access functions here (Table 3-14).
Table 3-14. Visual Basic’s intrinsic file-access functions
|
Category |
Function |
Use to |
|---|---|---|
|
Access |
|
Close an open file |
|
|
Copy a file | |
|
|
Get a file number for | |
|
|
Prevent others from accessing all or part of a file | |
|
|
Get the length of an open file in bytes | |
|
|
Open a file | |
|
|
Close all open files | |
|
Attributes |
|
Get the attributes of an open file |
|
|
Get the date that a file was created or changed | |
|
|
Get the length of a file ... |
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access