文本正在被过度写入时,它应该被追加(Text is being over written when

2019-09-18 08:13发布

这似乎很容易(我已经做到了一万次,从来没有问题),但它的杀了我。

我想基于Excel电子表格内容来创建一些SQL脚本。 要做到这一点我创建了一个宏,使用下面的代码读取文本文件

Dim fso As FileSystemObject
Set fso = New FileSystemObject

Dim stream As TextStream
Set stream = fso.OpenTextFile(filepath, 8, False)

应该打开文本文件进行追加和插件在我的新的价值观。

不幸的是,它总是覆盖 ,而不是附加,和它的驾驶我坚果。

有任何想法吗?

Answer 1:

最近,我刚刚建立了一个功能字符串追加到文件中。 短短几周/几个月前,我碰到这个问题,并发现,如果使用的实际词ForAppending,只是因为它在智能感知显示出来,insted的它为我工作8号的。

Const ForAppending = 8

Sub AppendStringToFile(ByVal strFile As String, ByVal strNewText As String, Optional intBlankLine As Integer = 1)

Dim fso as FileSystemObject, ts as TextStream

Set fso = New FileSystemObject
Set ts = fso.OpenTextFile(strFile, ForAppending, True)

With ts
    .WriteBlankLines intBlankLine
    .WriteLine (strNewText)
    .Close
End With

Set ts = Nothing
Set fso = Nothing

End Sub


Answer 2:

回落至基础....
Open pathname For mode [Access access] [lock] As [#]filenumber [Len=reclength]

使用您的要求:

Dim FNum As Integer

FNum = FreeFile()
Open strFile For Append As FNum

'do your stuff here
Write #FNum, MyData

Close FNum


Answer 3:

这是非常奇怪,在本文档中,提到对应于恒定ForAppending8 ,但它使用3在实施例在底部。

尝试:

Dim fso As FileSystemObject
Set fso = New FileSystemObject

Dim stream As TextStream
Set stream = fso.OpenTextFile(filepath, 3, False)


文章来源: Text is being over written when it's supposed to be appended