追加文本文件
你好,我想将一些数据附加到现有的txt文件中。没什么大不了的。
Sub OpenTextFileTest
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Dim fs, f
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile("c:\testfile.txt", ForAppending,TristateFalse)
f.Write "Hello world!"
f.Close
End Sub
这就是de Helpfile所说的。
无效的过程调用或参数
是我得到的错误消息。
这里出了什么问题。
提前致谢
Bernd
**** Hidden Message ***** 好的,有几件事...
常量确实应该在过程的范围之外定义。
如果程序要在本地使用,只需将文件系统对象硬引用到项目中,即“Microsoft Script Host Object Model”
您在调用中缺少一个参数(如果不存在则创建)
工作示例是:
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Public Sub OpenTextFileTest()
Dim fs As New FileSystemObject
Dim f
Set f = fs.OpenTextFile("c:\testfile.txt", ForAppending, True, TristateFalse)
F.Write "Hello World!"
F.Close
End Sub
请记住,这不会追加回车,因此如果您希望数据追加到下一行,您应该将vbCr或vbCrLf写入文件。 我的帮助中没有列出“写”选项....并且Appending的常数应该是8,而不是3。 谢谢,
这就是我在Acad2002
Constant Value Description
ForReading 1 Open a file for reading only. You can't write to this file.
ForAppending 8 Open a file and write to the end of the file.
The format argument can have any of the following settings:
Constant Value Description
TristateUseDefault 2 Opens the file using the system default.
TristateTrue 1 Opens the file as Unicode.
TristateFalse 0 Opens the file as ASCII.
Remarks
The following code illustrates the use of the OpenTextFile method to open a file for appending text:
Sub OpenTextFileTest
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Dim fs, f
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile("c:\testfile.txt", ForAppending,TristateFalse)
f.Write "Hello world!"
f.Close
End Sub
我的错误,我确实复制了,读起来不够好。抱歉,这是阿姆斯特丹糟糕的办公日结束。但它有效,感谢你们。下次我会在我哭泣之前更仔细地阅读。
贝恩德 我想你是对的,杰夫。
但我发现自己试图偷偷通过这个VBA挑战,我把自己投入其中(只是因为开始退出事件),而不是理解而是复制,这就是让我对自己失望的原因。
贝恩德 为什么引用“微软脚本宿主对象模型”?为什么不做这样的事情呢?
Private Sub AcadDocument_BeginClose()
Dim strFileName As String
Dim strDwgName As String
Dim strUser As String
Dim strDateTime As Date
Dim strWriteLine As String
strFileName = "filename.csv"
strDateTime = Date$ & " " & Time
strDwgName = ThisDrawing.GetVariable("dwgprefix") & ThisDrawing.GetVariable("dwgname")
strUser = ThisDrawing.GetVariable("loginname")
strWriteLine = strDateTime & "," & "close" & "," & strDwgName & "," & strUser
Dim intFile As Integer
intFile = FreeFile
Open "C:\Documents and Settings\user\My Documents\somefolder\" & strFileName For Append As #intFile
Print #intFile, strWriteLine
Close #intFile
End Sub
页:
[1]