Trying to append to file using an array

S

Susan Hayes

Hi
Im trying to append the info in an array which has numbers and letters. I cannot get the array to write to it.
This does work because Ive tried the appending "hello World" to it. It also appends the ","
Any help is appreciated
Thanks
And Thanks again for the many helps this group has provided to me
Mike

Dim myarray(19) as Variant

Dim fs, f, ts, s, chk
Set fs = CreateObject("Scripting.FileSystemObject")
chk = fs.FileExists("c:\data.txt")

If chk = True Then ' check if file exists
Set f = fs.GetFile("c:\data.txt")
Set ts = f.OpenAsTextStream(8, 0)
ts.write "Hello World"
For i = 1 To 19
ts.write myarray(i) & ","
Next
ts.write vbCrLf
ts.Close
Else 'if not create file
fs.CreateTextFile "c:\data.txt" 'creates file
Set f = fs.GetFile("c:\data.txt")
Set ts = f.OpenAsTextStream(8, 0)
ts.write "Hello World"
For i = 1 To 19
ts.write myarray(i) & ","
Next
ts.write vbCrLf
ts.Close
End If
 
N

NickHK

Susan,
Any good reason why you are using the FSO in VBA ?

If not, then check out VBA's "Open" statement. It will be more simple
because:
"If the file specified by pathname doesn't exist, it is created when a file
is opened for Append, Binary, Output, or Random modes."

NickHK

Susan Hayes said:
Hi
Im trying to append the info in an array which has numbers and letters. I
cannot get the array to write to it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top