Sub WriteToTextFile(FileUrl, mylist As Collection, CharSet)
Set Stm = CreateObject("adodb.stream")
Set b = CreateObject("ADODB.Recordset")
'MsgBox (Stm.EOS())
Stm.Type = 2
Stm.Mode = 3
Stm.CharSet = CharSet
Stm.Open
For Each myRow In mylist
'MsgBox (Stm.EOS)
Stm.WriteText myRow
Next
Stm.SaveToFile FileUrl, 2
Stm.flush
Stm.Close
Set Stm = Nothing
End Sub
Set Stm = CreateObject("adodb.stream")
Set b = CreateObject("ADODB.Recordset")
'MsgBox (Stm.EOS())
Stm.Type = 2
Stm.Mode = 3
Stm.CharSet = CharSet
Stm.Open
For Each myRow In mylist
'MsgBox (Stm.EOS)
Stm.WriteText myRow
Next
Stm.SaveToFile FileUrl, 2
Stm.flush
Stm.Close
Set Stm = Nothing
End Sub
本文介绍了一个使用VBA进行文件写入操作的例子。通过创建ADODB.Stream对象并设置其类型、模式及字符集,可以将指定的集合内容逐条写入到文本文件中。此方法适用于批量数据写入场景。
653

被折叠的 条评论
为什么被折叠?



