乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 78|回复: 5

追加文本文件

[复制链接]

32

主题

142

帖子

5

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
270
发表于 2005-7-7 10:47:05 | 显示全部楼层 |阅读模式
你好,
我想将一些数据附加到现有的txt文件中。没什么大不了的。
  1. Sub OpenTextFileTest
  2.     Const ForReading = 1, ForWriting = 2, ForAppending = 3
  3.     Dim fs, f
  4.     Set fs = CreateObject("Scripting.FileSystemObject")
  5.     Set f = fs.OpenTextFile("c:\testfile.txt", ForAppending,TristateFalse)
  6.     f.Write "Hello world!"
  7.     f.Close
  8. End Sub

这就是de Helpfile所说的。
无效的过程调用或参数
是我得到的错误消息。
这里出了什么问题。
提前致谢
Bernd
[/quote]

本帖以下内容被隐藏保护;需要你回复后,才能看到!

游客,如果您要查看本帖隐藏内容请回复
回复

使用道具 举报

154

主题

1274

帖子

8

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1936
发表于 2005-7-7 11:36:25 | 显示全部楼层
好的,有几件事...
常量确实应该在过程的范围之外定义。
如果程序要在本地使用,只需将文件系统对象硬引用到项目中,即“Microsoft Script Host Object Model”
您在调用中缺少一个参数(如果不存在则创建)
工作示例是:
  1. Const ForReading = 1
  2. Const ForWriting = 2
  3. Const ForAppending = 8
  4. Public Sub OpenTextFileTest()
  5. Dim fs As New FileSystemObject
  6. Dim f
  7. Set f = fs.OpenTextFile("c:\testfile.txt", ForAppending, True, TristateFalse)
  8. F.Write "Hello World!"
  9. F.Close
  10. End Sub

请记住,这不会追加回车,因此如果您希望数据追加到下一行,您应该将vbCr或vbCrLf写入文件。
回复

使用道具 举报

71

主题

928

帖子

8

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1230
发表于 2005-7-7 11:39:30 | 显示全部楼层
我的帮助中没有列出“写”选项....并且Appending的常数应该是8,而不是3。
回复

使用道具 举报

154

主题

1274

帖子

8

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1936
发表于 2005-7-7 11:44:18 | 显示全部楼层
谢谢,
这就是我在Acad2002
  1. Constant Value Description
  2. ForReading 1 Open a file for reading only. You can't write to this file.
  3. ForAppending 8 Open a file and write to the end of the file.
  4. The format argument can have any of the following settings:
  5. Constant Value Description
  6. TristateUseDefault –2 Opens the file using the system default.
  7. TristateTrue –1 Opens the file as Unicode.
  8. TristateFalse   0 Opens the file as ASCII.
  9. Remarks
  10. The following code illustrates the use of the OpenTextFile method to open a file for appending text:
  11. Sub OpenTextFileTest
  12.     Const ForReading = 1, ForWriting = 2, ForAppending = 3
  13.     Dim fs, f
  14.     Set fs = CreateObject("Scripting.FileSystemObject")
  15.     Set f = fs.OpenTextFile("c:\testfile.txt", ForAppending,TristateFalse)
  16.     f.Write "Hello world!"
  17.     f.Close
  18. End Sub

我的错误,我确实复制了,读起来不够好。抱歉,这是阿姆斯特丹糟糕的办公日结束。但它有效,感谢你们。下次我会在我哭泣之前更仔细地阅读。
贝恩德
回复

使用道具 举报

32

主题

142

帖子

5

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
270
发表于 2005-7-7 14:52:13 | 显示全部楼层
我想你是对的,杰夫。
但我发现自己试图偷偷通过这个VBA挑战,我把自己投入其中(只是因为开始退出事件),而不是理解而是复制,这就是让我对自己失望的原因。
贝恩德
回复

使用道具 举报

71

主题

928

帖子

8

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1230
发表于 2005-7-7 15:02:01 | 显示全部楼层
为什么引用“微软脚本宿主对象模型”?为什么不做这样的事情呢?
  1. Private Sub AcadDocument_BeginClose()
  2. Dim strFileName As String
  3. Dim strDwgName As String
  4. Dim strUser As String
  5. Dim strDateTime As Date
  6. Dim strWriteLine As String
  7. strFileName = "filename.csv"
  8. strDateTime = Date$ & " " & Time
  9. strDwgName = ThisDrawing.GetVariable("dwgprefix") & ThisDrawing.GetVariable("dwgname")
  10. strUser = ThisDrawing.GetVariable("loginname")
  11. strWriteLine = strDateTime & "," & "close" & "," & strDwgName & "," & strUser
  12. Dim intFile As Integer
  13. intFile = FreeFile
  14. Open "C:\Documents and Settings\user\My Documents\somefolder" & strFileName For Append As #intFile
  15. Print #intFile, strWriteLine
  16. Close #intFile
  17. End Sub
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-7-4 01:37 , Processed in 2.326462 second(s), 64 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表