是否获取通过VBA编辑Excel的最后一个用户?

w8f9ii69  于 2023-01-27  发布在  其他
关注(0)|答案(3)|浏览(181)

我想在Excel文件中显示最后一位编辑的姓名,因为有一些模型可供所有部门成员使用。
是否可以通过Excel VBA获得最后一位编辑Excel的编辑者的姓名?

1u4esq0p

1u4esq0p1#

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

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”模块中插入代码:

ycl3bljg

ycl3bljg2#

您可能会受益于内置属性“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"

lokaqttq

lokaqttq3#

您可以为事件Workbook_Open创建宏,该宏在某些日志文件中写入当前用户名。在https://support.microsoft.com上,它们具有用于获取当前用户名的sub

' 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

相关问题