Common syntax: object.CreateTextFile(filename[, overwrite[, unicode]])
. Here
- overwrite:
Optional. Boolean value that indicates whether you can overwrite an existing file. The value is
true
if the file can be overwritten, false
if it can't be overwritten. If omitted, existing files are not overwritten. Note:If the overwrite argument is false
, or is not provided, for a filename that already exists, an error occurs.
- unicode:
Optional. Boolean value that indicates whether the file is created as a Unicode or ASCII file. The value is
true
if the file is created as a Unicode file, false
if it's created as an ASCII file. If omitted, an ASCII file is assumed. (but last statement seems to contradict your experience...)
So you could open your file with
Set myFile = objFSO.CreateTextFile("C:\TestFile.txt", true, false)
If it fails, use object.OpenTextFile(filename[, iomode[, create[, format]]])
, where
- object: Required. Object is always the name of a FileSystemObject.
- filename: Required. String expression that identifies the file to
open.
- iomode: Optional. Can be one of three constants:
ForReading
,
ForWriting
, or ForAppending
.
- create: Optional. Boolean value that indicates whether a new file can
be created if the specified filename doesn't exist. The value is
True
if a new file is created, False
if it isn't created. If omitted, a
new file isn't created.
- format: Optional. One of three
TriState
values used to indicate the
format of the opened file. If omitted, the file is opened as ASCII.
You could use literal values for iomode
, create
and format
arguments, or define (and use) next constants:
'various useful constants
'iomode
Const ForReading = 1, ForWriting = 2, ForAppending = 8
'create
Const DontCreate = False ' do not create a new file if doesn't exist
Const CreateFile = True ' create a new file if the specified filename doesn't exist
'format
Const OpenAsDefault = -2 ' Opens the file using the system default.
Const OpenAsUnicode = -1 ' Opens the file as Unicode.
Const OpenAsUSAscii = 0 ' Opens the file as ASCII.
'TriState (seen in documetation)
Const TristateUseDefault = -2 ' Opens the file using the system default.
Const TristateTrue = -1 ' Opens the file as Unicode.
Const TristateFalse = 0 ' Opens the file as ASCII.
Then you could open your file with (but ensure to delete existing one if exists!)
Set myFile = objFSO.OpenTextFile( "C:\TestFile.txt", 2, true, 0)
or (more readable)
Set myFile = objFSO.OpenTextFile( "C:\TestFile.txt" _
, ForWriting, CreateFile, OpenAsUSAscii)