Access the answers to your technology questions today.
Subscribe Now
30-day free trial. Register in 60 seconds.
What Makes Experts Exchange Unique?
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.
Try it out and discover for yourself.
Subscribe Now
30-day free trial. Register in 60 seconds.
Join the Community
Give a Little. Get a Lot.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Join the Community
by: vhpcompPosted on 2005-06-28 at 10:43:15ID: 14320963
There doesn't seem to be a way to do this using VBA within the workbook, however some tips are here:
.com/Pages / T0510_Who _Has_the_F ile_Open.h tml
s").open
).open
s").open readonly :=true 'open read only in case someone is trying to write to it . value
http://exceltips.vitalnews
I'm not familar with the .net, so I'll take a stab at VBA using an additional tracker workbook.
As suggested in the article, you could use another workbook "user_tracker.xls" that has two columns, the file name and "current user"
like this
A1 B1
myworkbook.xls JoeUser
myworkbook2.xls SallyUser
The username in column b is overwritten each time the file is open
'First, in the workbook you are tracking you need code to get the user name.
'It is better to use Windows API than application.username, since that won't necessarily match
'network login. These two functions must go in the same module of that workbook:
Private Declare Function GetUserName Lib "advapi32.dll" _
Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) _
As Long
Function SystemUserName() As String
' Returns the name of the logged-in user
Dim Buffer As String * 100
Dim BuffLen As Long
BuffLen = 100
GetUserName Buffer, BuffLen
SystemUserName = Left(Buffer, BuffLen - 1)
End Function
'This sub goes in the Open event of the workbook you are tracking:
Private Sub Workbook_Open()
WriteUserName
End Sub
'this goes in another module
Sub WriteUserName()
dim sUser as string
Workbooks("user_tracker.xl
With activeworkbook
.Range("b1").value =SystemUserName 'this calls function and returns string
End With
Activeworkbook.close
End sub
'This code goes in the button click event:
Dim sCurUser as string
Workbooks("myworkbook.xls"
If activework.readonly = true 'then someone has it open, so check to see who
'close it here if you don't want it to remain open
Workbooks("user_tracker.xl
sCurUser = activeworkbook.range("B1")
ActiveWorkbook.close 'close the tracker workbook
msgbox("Workbook is currently opened by " & sCurUser")
End If
You may have to experiment to make sure the timing doesn't cause a problem if the files are being opened and closed very frequently.