VBA Track file usage

后端 未结 2 874
礼貌的吻别
礼貌的吻别 2021-01-23 02:40

I currently have an excel file that produces client statements. I need to track who has run their statements. Currently, whenever statements are produced I have a macro that sen

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-23 03:05

    If there is a location on your network that everyone has access to you can write a log file. Most likely a spot on the Sharepoint server.

    Something like this called from the code that is currently sending out the email.

    In you VBA IDE go to the tools menu and select references. Select "Microsoft scripting runtime"

    Private Sub LogUsage()
    
        Dim ts As TextStream
        Dim fs As FileSystemObject
        Dim strLogFile As String
    
        strLogFile = "\\servername\sharename\log\Usage.txt"
    
        'Check if the file exists, if it does, open it, if it doesn't create it
        Set fs = New FileSystemObject
        If fs.FileExists(strLogFile) = True Then
            Set ts = fs.OpenTextFile(strLogFile, ForAppending)
        Else
            Set ts = fs.CreateTextFile(strLogFile, True, False)
        End If
    
        'Log your entry
        ts.WriteLine "Used by " & Environ$("Username") & " at " & Now & " on computer " & Environ$("Computername")
    
         'Clean up
         ts.Close: Set ts = Nothing
         Set fs = Nothing
    
     End Sub
    

提交回复
热议问题