Solved

Temp txt file

Posted on 2011-03-11
2
309 Views
Last Modified: 2012-06-27
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
Comment
Question by:Seamus2626
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
ID: 35107441
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
 

Author Closing Comment

by:Seamus2626
ID: 35107459
Thanks Rorya!!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question