Windows API to get network username of person with file open

P

Paul Martin

Hi

I am writing code to open another Excel file. If the file is in use by
another user, I would like to use an API function to return the network
username of the person who has the file open. Any suggestions?

Thanks in advance

Paul Martin Melbourne, Australia
 
B

Bob Phillips

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


--

HTH

RP
(remove nothere from the email address if mailing direct)
 
P

Paul Martin

Hi Bob

Thanks for the reply. Firstly, WriteReserved returns False, and
secondly, WriteReservedBy returns the Excel username. The problem with
the latter is that this does not identify the actual username. I would
like to get the network username, hence the need for an API. I've
found an API from the newsgroups that returns the network name:

Private Declare Function apiGetUserName Lib "advapi32.dll" Alias
"GetUserNameA" _
(ByVal lpBuffer As String, nSize As Long) As Long

I can use this to get my username, but I want to get the username of
the user who has the file open that I am attempting to open through
code.

Regards

Paul
 
A

Alan

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
 
P

Paul Martin

Alan

That function will return MY network login. I want the network
username of the person who has a file open that I am programatically
opening.

Paul
 
D

Dave Peterson

I'm not sure if it's possible, but you may want to try posting the same question
in one of the VB newsgroups.

Well, if no one gives an acceptable answer here.
 
P

Paul Martin

It is possible using either NetFileGetInfo or NetFileEnum APIs (I'm not
sure which), but I haven't been able to find a suitable wrapper to
utilise them.
 
D

Dave Peterson

I'd try google or the VB newsgroups.

Paul said:
It is possible using either NetFileGetInfo or NetFileEnum APIs (I'm not
sure which), but I haven't been able to find a suitable wrapper to
utilise them.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top