Bob Phillips said:
You should be able to get away without APIs.
Open the workbook Read only, then check the WriteReservedBy
property of the workbook.
With ActiveWorkbook
If .WriteReserved = True Then
MsgBox "Opened by " & .WriteReservedBy
End If
End With
I could be wrong, but doesn't that just give you the name that
the machine that has opened the file has entered in
excel - tools - options?
It *should* be correct, but isn't definitively the user who is
logged in.
Of course, you could also argue that someone could log in under a
different network username anyway!
An example of the Win32 API is as follows (I think I got this from
this newsgroup, but as is often the case, I didn't record the original
source, so apologies to the OP of this function and thanks too!):
+-+-++-+-++-+-++-+-++-+-++-+-+-
Private Declare Function GetNetworkUserName Lib "advapi32.dll" Alias
"GetUserNameA" _
(ByVal lpBuffer As String, _
ByRef nSize As Long) As Long
'Get the user's login ID
Function NetworkUserName() As String
'A buffer that the API function fills with the login name
Dim sBuffer As String * 255
'Variable to hold the length of the buffer
Dim lStringLength As Long
'Initialize to the length of the string buffer
lStringLength = Len(sBuffer)
'Call the API function, which fills the buffer
'and updates lStringLength with the length of the login ID,
'including a terminating null - vbNullChar - character
GetNetworkUserName sBuffer, lStringLength
If lStringLength > 0 Then
'Return the login id, stripping off the final vbNullChar
NetworkUserName = Left$(sBuffer, lStringLength - 1)
End If
End Function
+-+-++-+-++-+-++-+-++-+-++-+-+-
HTH,
Alan.
--
The views expressed are my own, and not those of my employer or anyone
else associated with me.
My current valid email address is:
(e-mail address removed)
This is valid as is. It is not munged, or altered at all.
It will be valid for AT LEAST one month from the date of this post.
If you are trying to contact me after that time,
it MAY still be valid, but may also have been
deactivated due to spam. If so, and you want
to contact me by email, try searching for a
more recent post by me to find my current
email address.
The following is a (probably!) totally unique
and meaningless string of characters that you
can use to find posts by me in a search engine:
ewygchvboocno43vb674b6nq46tvb