Consulting

Results 1 to 4 of 4

Thread: Add new line to .txt file

  1. #1
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location

    Add new line to .txt file

    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
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  2. #2
    Knowledge Base Approver VBAX Expert brettdj's Avatar
    Joined
    May 2004
    Location
    Melbourne
    Posts
    649
    Location
    John, try

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

    Cheers

    Dave

  3. #3
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    This should work.


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

  4. #4
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Thanx dave - I just knew there had to be something simpler than what I'd been trying

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

    Many thanx,
    John
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •