确定谁在Sharepoint上打开Excel文件

时间:2014-11-12 21:26:43

标签: excel excel-vba sharepoint vba

我理解“Environ”可以识别谁打开文件,但我不知道如何为其编写代码。

我发现一个答案是在打开文件时通过Outlook发送电子邮件,但理想情况下,它会记录在工作表或其他文件的隐藏选项卡中的人名和时间戳。由于用户不会对文件进行编辑和/或保存,我不知道这是否是一种选择。

1 个答案:

答案 0 :(得分:0)

以下是您可以使用的一些代码。打开VBE(Alt + F11)双击" ThisWorkbook"在电子表格的“项目”窗口中,然后将其粘贴到。

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

    'When the worksheet opens, this will write the computer username
    '   and the date and time to a worksheet of your choice
    '   just change that "YourHiddenSheetNameHere" to the name of your
    '   hidden tab

    Dim lastRow As Integer
    Dim hiddenSheet As Worksheet

    Set hiddenSheet = Sheets("YourHiddenSheetNAmeHere")
    lastRow = hiddenSheet.Range("A999999").End(xlUp).Row

    hiddenSheet.Cells(lastRow, 1).Value = ReturnUserName
    hiddenSheet.Cells(lastRow, 1).Value = Now()


End Function

Function ReturnUserName() As String
     ' returns the NT Domain User Name
    Dim rString As String * 255, sLen As Long, tString As String
    tString = ""
    On Error Resume Next
    sLen = GetUserName(rString, 255)
    sLen = InStr(1, rString, Chr(0))
    If sLen > 0 Then
        tString = Left(rString, sLen - 1)
    Else
        tString = rString
    End If
    On Error GoTo 0
    ReturnUserName = UCase(Trim(tString))
End Function

每次有人打开工作簿时都会触发,保存用于登录计算机的用户名以及日期和时间。保存到您坚持的任何标签。您需要使用.xlsm而非.xlsx保存工作簿,因为它将是一本支持宏的书。