ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Windows API to get network username of person with file open (https://www.excelbanter.com/excel-programming/346909-windows-api-get-network-username-person-file-open.html)

Paul Martin

Windows API to get network username of person with file open
 
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


Bob Phillips[_6_]

Windows API to get network username of person with file open
 
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)


"Paul Martin" wrote in message
ups.com...
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




Paul Martin

Windows API to get network username of person with file open
 
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


Alan

Windows API to get network username of person with file open
 
"Bob Phillips" wrote in message

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:



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




Paul Martin

Windows API to get network username of person with file open
 
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


Dave Peterson

Windows API to get network username of person with file open
 
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.

Paul Martin wrote:

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


--

Dave Peterson

Paul Martin

Windows API to get network username of person with file open
 
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.


Dave Peterson

Windows API to get network username of person with file open
 
I'd try google or the VB newsgroups.

Paul Martin wrote:

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.


--

Dave Peterson


All times are GMT +1. The time now is 05:41 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com