Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Temp txt file

Posted on 2011-03-11
2
Medium Priority
?
353 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 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
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.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

609 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