Author Topic: Append textfile  (Read 4090 times)

0 Members and 1 Guest are viewing this topic.

Amsterdammed

  • Guest
Append textfile
« on: July 07, 2005, 10:47:05 AM »
Hello ebverybody


I want to append some data to a existing txt file. No big deal ithought.

Code: [Select]


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


this is what even de Helpfile says.


Invalid procedure call or argument

is what i get as an error msg.

What is going wrong here.

Thanks in Advance

Bernd

[/quote]

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Append textfile
« Reply #1 on: July 07, 2005, 11:36:25 AM »
Ok, a few things ...

Constants really should be defined outside of the scope of a procedure.

If the program is to be used locally, simply hard reference the file system object into the project i.e. "Microsoft Script Host Object Model"

You are missing a parameter in the call (to create if it does not exist)

Working example is:
Code: [Select]


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



Remember that this does NOT append carriage returns so if you want the data to append to the next line, you should write vbCr or vbCrLf to the file.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Jeff_M

  • King Gator
  • Posts: 4094
  • C3D user & customizer
Append textfile
« Reply #2 on: July 07, 2005, 11:39:30 AM »
The Help I have does not list a "ForWriting" option....and the constant ForAppending should be 8, not 3.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Append textfile
« Reply #3 on: July 07, 2005, 11:44:18 AM »
Indeed ....

ForAppending is 8
ForWriting is 2
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Amsterdammed

  • Guest
Append textfile
« Reply #4 on: July 07, 2005, 02:52:13 PM »
Thanks,

That is the Help i have in Acad2002
Code: [Select]

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



My mistake, I did copy and not read good enough. Sorry, it was the end of  a bad office day here in Amsterdam. But it works, thanks to you folks. Next time I will read more carefully before I cry for Help.

Bernd :oops:

Jeff_M

  • King Gator
  • Posts: 4094
  • C3D user & customizer
Append textfile
« Reply #5 on: July 07, 2005, 03:02:01 PM »
Heh, I didn't look at the entire help page. I just looked agian and, sure enough, the example they show is in direct violation of the data given above......can't even blame Adesk for this, as it's a MS created help........

I wouldn't be embarrassed, too much anyway, Bernd. After all, isn't the help supposed to HELP you, not confuse you more than before you read it? :P

Amsterdammed

  • Guest
Append textfile
« Reply #6 on: July 07, 2005, 03:21:39 PM »
I guess you are right, Jeff.

But I catch myself trying to sneak through this VBA challenge I put my self into (just because of the BeginQuit event) with not understanding but copying, that is what  made me a bid disappointed by myself.

Bernd

Bob Wahr

  • Guest
Re: Append textfile
« Reply #7 on: September 06, 2005, 04:05:53 PM »
Why do the "Microsoft Script Host Object Model" reference?  Why not just do something like this?
Code: [Select]
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