打开文本文件并附加新文本?

打开文本文件并附加新文本?

我创建了一个文本,然后想将文本附加到其中。但我收到错误:无效的过程调用或参数。

Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim Fileout As Object
Dim filePath As String
filePath = "C:\myFile.txt"


If FileExists(filePath) = False Then
Set Fileout = fso.CreateTextFile(filePath, True, True)
Fileout.Write Msg
Fileout.Close
Else
Set Fileout = fso.OpenTextFile(filePath, ForAppending, TristateFalse) <<<<<== ERROR HERE
Fileout.Write Msg
Fileout.Close
End If

Function FileExists(strFullPath As String) As Boolean
'Check if a file or folder exists
If Not Dir(strFullPath, vbDirectory) = vbNullString Then FileExists = True
End Function

答案1

我收到一个错误:无效的程序或参数。

Set Fileout = fso.OpenTextFile(filePath, ForAppending, TristateFalse)

您正在将TristateFalse其作为参数 3 传递。它应该是参数 4。

请阅读文档。

描述

打开指定的文件并返回可用于读取或附加到文件的 TextStream 对象。

句法

object.OpenTextFile(filename[, iomode[, create[, format]]])

OpenTextFile 方法包含以下部分:

Part      Description
----      -----------
object    Required. Always the name of a FileSystemObject.
filename  Required. String expression that identifies the file to open.
iomode    Optional. Indicates input/output mode. Can be one of two constants, either ForReading 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. The default is False.
format    Optional. One of three Tristate values used to indicate the format of the opened file. If omitted, the file is opened as ASCII.

来源OpenTextFile 方法

相关内容