I have a excel based tool which I have shared with many users and I want to log the usage of the tool like who has used it and some other specifics of the tool like its settings, etc.
Private Sub UserLog()
Dim UserName() As String
Dim SessionID As String
Dim outApp As Object, outSession As Object
Dim currentUserEmailAddress As String
Dim UserTrackingLastRow As Integer
currentUserEmailAddress = ""
On Error Resume Next
Set outApp = CreateObject("Outlook.Application")
If outApp Is Nothing Then
currentUserEmailAddress = "Cannot create Microsoft Outlook session."
currentUserEmailAddress = "Not found"
Else
'Set a NameSpace object variable with .Session property (same as .GetNamespace("MAPI"), to access existing Outlook items, and get
'current user name
Set outSession = outApp.Session.CurrentUser
'Get current user email address
currentUserEmailAddress = outSession.AddressEntry.GetExchangeUser().PrimarySmtpAddress
Set outApp = Nothing
End If
‘#####################################################################
sfilename = "Excel file path ON ONEDRIVE"
'Set xl = CreateObject("Excel.Sheet")
Set xl = CreateObject("Excel.application")
xl.Visible = False
Set xlsheet = xl.Application.Workbooks.Open(Filename:=sfilename, ReadOnly:=False, IgnoreReadOnlyRecommended:=True)
UserTrackingLastRow = xlsheet.Sheets("Sheet1").Cells(Rows.Count, 2).End(xlUp).Row
'Create Session ID with User Name initials and latest sequence
UserName = Split(Application.UserName, ",")
SessionID = Trim(Left$(UserName(1), 2)) & Left$(UserName(0), 1) & UserTrackingLastRow
Sheets("LM Tool Data Collection").Cells(10002, 10000).Value = SessionID
xlsheet.Sheets("Sheet1").Range("B" & UserTrackingLastRow + 1) = SessionID
xlsheet.Sheets("Sheet1").Range("C" & UserTrackingLastRow + 1) = Application.UserName
xlsheet.Sheets("Sheet1").Range("D" & UserTrackingLastRow + 1) = currentUserEmailAddress
xlsheet.Sheets("Sheet1").Range("E" & UserTrackingLastRow + 1) = Now
xlsheet.Sheets("Sheet1").Range("F" & UserTrackingLastRow + 1) = ThisWorkbook.Names("A").RefersToRange.Value
xlsheet.Sheets("Sheet1").Range("G" & UserTrackingLastRow + 1) = ThisWorkbook.Names("B").RefersToRange.Value
xlsheet.Sheets("Sheet1").Range("H" & UserTrackingLastRow + 1) = ThisWorkbook.Names("C).RefersToRange.Value
xlsheet.Sheets("Sheet1").Range("I" & UserTrackingLastRow + 1) = ThisWorkbook.Names("D).RefersToRange.Value
xlsheet.Sheets("Sheet1").Range("J" & UserTrackingLastRow + 1) = ThisWorkbook.Names("E").RefersToRange.Value
xlsheet.Close SaveChanges:=True
End Sub
Now if I use the above code, I need to grant access to each user so that their data is logged and they also have access to the log. I want to have a Microsoft form which will log it and the form will be saved in the excel for me to access it.