PDA

View Full Version : [SOLVED:] Add new line to .txt file



johnske
05-01-2005, 05:37 PM
Hi,

I've been playing around with the idea of having a 'hidden' text file to secretly log all the dates and times that a workbook has been opened. I have the code below, but this only writes the last time it's been opened (overwriting the previous entry)...

So - I want to know how you add a new line to the text file (leaving the previous entries unchanged) to insert the current time/date?


Private Sub Workbook_Open()
Dim DateOpened$, NewFile$
DateOpened = Format(Now, "dd mmm yyyy hh:mm:ss")
On Error GoTo CreateFile
MakeEntry:
Open "c:\" & Left(ThisWorkbook.Name, _
Len(ThisWorkbook.Name) - 4) & _
" LogFile.txt" For Output As #1
Write #1, "Accessed: " & DateOpened
Close #1
Exit Sub
CreateFile:
If Err.Number = 53 Then '=LogFile file does not exist...
NewFile = "Create 'C:\" & Left(ThisWorkbook.Name, _
Len(ThisWorkbook.Name) - 4) & _
" LogFile.txt' File"
Resume MakeEntry
End If
Resume Next
End Sub

TIA
John

brettdj
05-01-2005, 06:18 PM
John, try


Open "c:\" & Left(ThisWorkbook.Name, _
Len(ThisWorkbook.Name) - 4) & _
" LogFile.txt" For Append As #1


Cheers

Dave

Jacob Hilderbrand
05-01-2005, 06:25 PM
This should work.



Open "c:\" & Left(ThisWorkbook.Name, _
Len(ThisWorkbook.Name) - 4) & _
" LogFile.txt" For Append As #1
Print #1, "Accessed: " & DateOpened
Close #1

johnske
05-01-2005, 06:28 PM
Thanx dave - I just knew there had to be something simpler than what I'd been trying :thumb

(I just got something from Jake, so I'll have a look at that next) - thanx Jake

Many thanx,
John