Logging data using the same textfile over and over again

  • Thread starter Thread starter Silencer116
  • Start date Start date
S

Silencer116

Hey all.

I am working on a log-like mechanism in VBA. I programmed the following
sub:

Sub CreateLog()

Datum = Date
Tijd = Time

Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("H:\test " + Cstr(Datum) + ".txt", True)
a.WriteLine("The process begun @ " + Cstr(Tijd) + " on " + Cstr(Datum))

a.close

End Sub

But this overwrites the textfile over and over again. I would like it
to open the existing textfile, and add a new line (the same line as
above) on top of the other line(s)

The result would be a summary of lines in one textfile. Everytime this
macro is activated i would like to be an line added.

is this possible? if yes, please could anyone help me?
 
Hello Silencer,

Yust need to append the text to the existing text file.

Const ForAppending = 8
Set objTextFile = fs.OpenTextFile (strDirectory & strFile,
ForAppending, True)
objTextFile.WriteLine "text"

Best Regards,

Agnieszka
 
You might be able to adapt the following, it creates a login record in a
text file (appends each time the workbook is opened). Does not use FSO
scripting either!



Private Sub Workbook_Open()
Open ThisWorkbook.Path & "\TrackUsage.log" For Append As #1
Print #1, Application.UserName, Now, "Opened"
Close #1
End Sub
 

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

Back
Top