VBA - Open Book Username (read-only)

If the open book (located on the server) is in read-only mode, how can I display the active username using VBA?

I looked .WriteReservedBy, but this only shows the name of the person who last saved the password file.

+4
source share
1 answer

This is probably a comment, but my reputation is too low.

I saw this, but did not need information ...

What you need to try:

  • ThisWorkbook.UserStatus - an array with all current users for an open file as exclusive or shared
  • Environ ("USERNAME")
  • CreateObject ("WScript.NetWork"). Username
  • API calls:

.

Declare Function WNetGetUser Lib "mpr.dll" Alias "WNetGetUserA"
( _
    ByVal lpName As String, _
    ByVal lpUserName As String, _
    lpnLength As Long
) As Long

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

.

More about these APIs:



Public Function GetActiveUser(Optional ByVal computer As String = ".") As String
    Dim wmi As Object, itm As String

    On Error Resume Next
    Set wmi = GetObject("winmgmts:\\" & computer & "\Root\CIMv2")
    itm = wmi.ExecQuery("Select UserName from Win32_NetworkConnection", , 48)
    GetNetActiveUser = itm
End Function
+1

All Articles