Elixir
Elixir

Reputation: 303

VBA - Username of open workbook (read only)

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

I've looked into .WriteReservedBy but this only shows the name of the person that last saved the file with a password.

Upvotes: 4

Views: 1902

Answers (1)

paul bica
paul bica

Reputation: 10715

This should probably be a comment but my reputation is too low

I've seen this but never needed the info...

Things to try:

  • ThisWorkbook.UserStatus - array with all current users for the file open 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 details 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

Upvotes: 1

Related Questions