VBA - 打开工作簿的用户名(只读)

时间:2015-05-26 18:21:03

标签: vba excel-vba ms-access access-vba excel

如果打开的工作簿(位于服务器上)处于只读模式,如何使用VBA显示活动用户名?

我查看了.WriteReservedBy,但这只显示了上次使用密码保存文件的人的姓名。

1 个答案:

答案 0 :(得分:1)

这应该是评论,但我的声誉太低

我已经看过这个但从未需要这些信息...

要尝试的事情:

  • ThisWorkbook.UserStatus - 该文件的所有当前用户以独占或共享方式打开的数组
  • ENVIRON(" USERNAME&#34)
  • 的CreateObject(" WScript.NetWork&#34)用户名
  • API调用:

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)

有关这些API的更多详细信息:

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