当文本被追加时,文本被覆盖 [英] Text is being over written when it's supposed to be appended

查看:133
本文介绍了当文本被追加时,文本被覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想创建一些SQL脚本基于Excel电子表格中的内容。为此,我创建了一个使用下面的代码读取文本文件的宏:

  Dim fso As FileSystemObject 
Set fso = New FileSystemObject

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

这是假设来打开文本文件来追加和插入我的新值。

<



有什么想法吗?

解决方案

我最近建立了一个函数来追加字符串到一个文件。我在短短几周/几个月前遇到过这个问题,发现如果使用实际的词 ForAppending ,就像在Intellisense中显示的那样,由数字 8 为我工作。
$ b $ pre $ Const ForAppending = 8

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

作为FileSystemObject的Dim fso,作为TextStream的ts

设置fso =新的FileSystemObject
Set ts = fso.OpenTextFile strFile,ForAppending,True)

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

Set ts = Nothing
Set fso = Nothing

End Sub


This seems really easy (I've done it a million times and never had a problem), but it's killing me.

I want to create some SQL scripts based on content in an Excel spreadsheet. To do this I've created a macro that reads a text file using the code below

Dim fso As FileSystemObject
Set fso = New FileSystemObject

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

This is supposed to open the text file for appending and plug in my new values.

Unfortunately, it's always overwriting instead of appending, and it's driving me nuts.

Any ideas?

解决方案

I just recently built a function to Append Strings to a File. I came across this issue just a few weeks / months ago and found that if used the actual word ForAppending, just as it shows up in Intellisense, insted of the number 8 it worked for me.

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

这篇关于当文本被追加时,文本被覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆