Message 1 of 14
append to text file - i give up

Not applicable
01-06-2002
02:57 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
arggggh! after wasting the better part of the last two days trying to do
this one simple thing I have to admit ignorance and ask for help
I've read and re-read the help files and searched archives and google and
can't fiind anything that has helped me understand what I'm doing wrong.
I've tried a hundred variations of getfile, createtextfile, open
statement[forWriting, ForReading, ForAppending], OpenAsTextStream,
OpenTextFile, writeline, readline etc and I cant seem to get a simple text
file and append to it.
I know this is pathetically lame that I can't figure this out on my own but
if there's any sympathy out there for a lamer...help!
my latest failed example is:
Public Sub AddToFile(filenm as string, msg As String)
'declare file locations
Dim FileLocation As String
FileLocation = "C:\temp\"
'declare file name variables
Dim FileName As String
'assign name to text file
FileName = FileLocation & filenm
'create a file object for the report file
Dim fso As Variant 'file scripting object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim objFile As Variant 'actual file textstream object
'create actual file stream object
On Error Resume Next
Set objFile = fso.GetFile(FileName)
If Err Then
Set objFile = fso.CreateTextFile(FileName, True) 'allow overwrite
Err.Clear
End If
Dim objfile2 As Variant
'this is supposed to open the file for appending but it doesn't
'supposedly ForAppending etc are built in constants but they aren't
'you apparently have to declare them as constants yourself
'the help says append = 3 but the example uses 8 but I can't get either to
work
Set objfile2 = objFile.OpenAsTextStream(8) 'forappending - or (3) -
different help files
'write msg to file
'since the file was "supposedly" opened for append this should add a new
line each time it's run
'but it doesn't
objfile.Writeline msg
objfile.Close
Set objFile = Nothing
Set objfile2 = Nothing
Set fso = Nothing
End Sub
that would then be called with:
Sub testx()
AddToFile "test1.txt", "this is a test"
End Sub
I know somehow I'm making this much harder than it has to be.
This has to be the easiest thing to do and I cant figure it out....dumb dimb
dimmmerer
Thanks for any help
Mark
this one simple thing I have to admit ignorance and ask for help
I've read and re-read the help files and searched archives and google and
can't fiind anything that has helped me understand what I'm doing wrong.
I've tried a hundred variations of getfile, createtextfile, open
statement[forWriting, ForReading, ForAppending], OpenAsTextStream,
OpenTextFile, writeline, readline etc and I cant seem to get a simple text
file and append to it.
I know this is pathetically lame that I can't figure this out on my own but
if there's any sympathy out there for a lamer...help!
my latest failed example is:
Public Sub AddToFile(filenm as string, msg As String)
'declare file locations
Dim FileLocation As String
FileLocation = "C:\temp\"
'declare file name variables
Dim FileName As String
'assign name to text file
FileName = FileLocation & filenm
'create a file object for the report file
Dim fso As Variant 'file scripting object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim objFile As Variant 'actual file textstream object
'create actual file stream object
On Error Resume Next
Set objFile = fso.GetFile(FileName)
If Err Then
Set objFile = fso.CreateTextFile(FileName, True) 'allow overwrite
Err.Clear
End If
Dim objfile2 As Variant
'this is supposed to open the file for appending but it doesn't
'supposedly ForAppending etc are built in constants but they aren't
'you apparently have to declare them as constants yourself
'the help says append = 3 but the example uses 8 but I can't get either to
work
Set objfile2 = objFile.OpenAsTextStream(8) 'forappending - or (3) -
different help files
'write msg to file
'since the file was "supposedly" opened for append this should add a new
line each time it's run
'but it doesn't
objfile.Writeline msg
objfile.Close
Set objFile = Nothing
Set objfile2 = Nothing
Set fso = Nothing
End Sub
that would then be called with:
Sub testx()
AddToFile "test1.txt", "this is a test"
End Sub
I know somehow I'm making this much harder than it has to be.
This has to be the easiest thing to do and I cant figure it out....dumb dimb
dimmmerer
Thanks for any help
Mark