• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 356
  • Last Modified:

Temp txt file

Hi,

The code below is not recording when users enter a spreadsheet, simply when they leave it.

Can anyone see why that is?

Thanks
Seamus
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Debug.Print "closed"
MyFile = "C:\Documents and Settings\travseam\Desktop\temp.txt"
'set and open file for output
fnum = FreeFile()
Open MyFile For Output As fnum
'write project info and then a blank line. Note the comma is required
Print #fnum, Environ("USERNAME") & " left at " & Now()
Write #fnum,
Close #fnum

End Sub

Private Sub Workbook_Open()
Debug.Print "open"
MyFile = "C:\Documents and Settings\travseam\Desktop\temp.txt"
'set and open file for output
fnum = FreeFile()
Open MyFile For Output As fnum
'write project info and then a blank line. Note the comma is required
Print #fnum, Environ("USERNAME") & " entered at " & Now()
Write #fnum,
Close #fnum
End Sub

Open in new window

0
Seamus2626
Asked:
Seamus2626
1 Solution
 
Rory ArchibaldCommented:
You are overwriting the file each time. Try this:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Debug.Print "closed"
WriteFile Environ("USERNAME") & " left at " & Now()
End Sub

Private Sub Workbook_Open()
Debug.Print "open"
WriteFile Environ("USERNAME") & " entered at " & Now()
End Sub
Sub WriteFile(strText As String)
   Dim MyFile As String, fNum As Integer
   MyFile = "C:\test\temp.txt"
   'set and open file for output
   fNum = FreeFile()
   Open MyFile For Append As fNum
   'write project info and then a blank line. Note the comma is required
   Print #fNum, strText
   Write #fNum,
   Close #fNum

End Sub

Open in new window

0
 
Seamus2626Author Commented:
Thanks Rorya!!
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now