【问题标题】:Get the last one who edit the excel through VBA?得到最后一个通过 VBA 编辑 excel 的人?
【发布时间】:2022-06-10 20:23:55
【问题描述】:

我想在 excel 文件中显示最后一个编辑者的名字。

是否可以通过Excel VBA获取最后编辑excel的编辑姓名?

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    您可以为事件 Workbook_Open 创建一个宏,将当前用户名写入某个日志文件。在https://support.microsoft.com 他们有一个子来获取当前用户名

    ' Makes sure all variables are dimensioned in each subroutine.     
    Option Explicit     
    ' Access the GetUserNameA function in advapi32.dll and     ' call the function GetUserName.     
    Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long    
     ' Main routine to Dimension variables, retrieve user name     
    ' and display answer.     
    Sub Get_User_Name()     
    ' Dimension variables    
     Dim lpBuff As String * 25     
    Dim ret As Long, UserName As String     
    ' Get the user name minus any trailing spaces found in the name.    
     ret = GetUserName(lpBuff, 25)     
    UserName = Left(lpBuff, InStr(lpBuff, Chr(0)) - 1)     
    ' Display the User Name     
    MsgBox UserName     
    End Sub
    

    【讨论】:

      【解决方案2】:

      您可能会受益于内置属性“last author”,每次保存都会刷新该属性,并且可以通过以下函数读取:

      
      Private Function LastAuthor() As String
          Dim prop As Object
          On Error Resume Next
          Set prop = ThisWorkbook.BuiltinDocumentProperties("last author")
          If Err.Number = 0 Then
              LastAuthor = prop.Value
          Else
              LastAuthor = "Not yet documented!"
          End If
      End Function
      

      另一个感兴趣的内置属性可能是"Last save time"。

      【讨论】:

        【解决方案3】:

        我认为最简单的方法是使用这个内置函数。

        Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
        
        User = Application.UserName
        
        'Save the user and probably date time in wherever you want
        
        End Sub
        

        您需要在“ThisWorkbook”模块中插入代码:

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-09-23
          • 2014-05-05
          • 1970-01-01
          • 1970-01-01
          • 2020-02-10
          • 2017-04-27
          相关资源
          最近更新 更多