|
| 1 | +Attribute VB_Name = "Logger" |
| 2 | + |
| 3 | +'--------------------------------------------------------------------------------------- |
| 4 | +' Module : Logger |
| 5 | +' Author : Patrick Rye |
| 6 | +' Date : 7/20/2015 |
| 7 | +' Purpose : Holds a function which will hold whatever is entered for it to a file. |
| 8 | +'--------------------------------------------------------------------------------------- |
| 9 | +Option Explicit |
| 10 | +' ---------------------------------------------------------------------------- |
| 11 | +Private Const gBlLogAction As Boolean = True 'Whether all actions should be logged or not |
| 12 | +Private Const strBaseLogFileName As String = "log.txt" 'The base name of the file |
| 13 | +Private Const gBlAppendDateToLog As Boolean = True 'Whether the date should be added to the front of the log file or not. |
| 14 | +Private Const strLogFolderPath As String = "" 'The file path for the folder the log is placed into, leave blank to save to same folder as excel file. |
| 15 | +' ---------------------------------------------------------------------------- |
| 16 | +'Use the line to call this sub, remove ' before and replace *Log* with what you want logged |
| 17 | +'Call WriteToLog(*Log*) |
| 18 | +' ---------------------------------------------------------------------------- |
| 19 | +Public Sub WriteToLog(strLine As String) |
| 20 | +Dim strLogFilePath As String |
| 21 | +Dim strUserName As String |
| 22 | +Dim fso As Object |
| 23 | +Set fso = CreateObject("Scripting.FileSystemObject") |
| 24 | +Dim oFile As Object |
| 25 | +If gBlLogAction <> True Then GoTo EndLogger 'If logging is not set to true then go to the end of the function |
| 26 | +Dim strDate As String |
| 27 | +Dim strTime As String |
| 28 | +strDate = DateMaker() 'Make the Date |
| 29 | +strTime = TimeMaker() 'Make the time |
| 30 | +strUserName = (Environ$("Username")) 'Get the user name from the environmental variables |
| 31 | +'Make the File Path of the log file. |
| 32 | +If strLogFolderPath <> "" Then |
| 33 | + strLogFilePath = strLogFolderPath |
| 34 | +Else |
| 35 | + strLogFilePath = ThisWorkbook.Path |
| 36 | +End If |
| 37 | +If Right(strLogFilePath, 1) <> "\" Then strLogFilePath = strLogFilePath & "\" |
| 38 | +If gBlAppendDateToLog Then strLogFilePath = strLogFilePath & strDate & " " |
| 39 | +strLogFilePath = strLogFilePath & strBaseLogFileName |
| 40 | + |
| 41 | +'Checks if the log file already exits or not. |
| 42 | +If Dir(strLogFilePath) <> "" Then |
| 43 | + 'File Does exist, open it for appending |
| 44 | + Set oFile = fso.OpenTextFile(strLogFilePath, 8, True) |
| 45 | +Else |
| 46 | + 'File does not exist, create it with headers |
| 47 | + Set oFile = fso.CreateTextFile(strLogFilePath) |
| 48 | + oFile.WriteLine "Date Time User | Action" |
| 49 | + oFile.WriteLine "-------------------------------------------------------------------------------------------------" |
| 50 | +End If |
| 51 | +oFile.WriteLine strDate & " " & strTime & " " & strUserName & " | " & strLine |
| 52 | +oFile.Close |
| 53 | +EndLogger: |
| 54 | +Set fso = Nothing |
| 55 | +Set oFile = Nothing |
| 56 | +End Sub |
| 57 | +' ---------------------------------------------------------------------------- |
| 58 | +Private Function DateMaker() As String |
| 59 | +'Writes the date in the following Format YYYY-MM-DD |
| 60 | +Dim strDate As String |
| 61 | +strDate = Year(Now) & "-" |
| 62 | +If Month(Now) < 10 Then |
| 63 | + strDate = strDate & "0" & Month(Now) & "-" |
| 64 | +Else |
| 65 | + strDate = strDate & Month(Now) & "-" |
| 66 | +End If |
| 67 | +If Day(Now) < 10 Then |
| 68 | + strDate = strDate & "0" & Day(Now) |
| 69 | +Else |
| 70 | + strDate = strDate & Day(Now) |
| 71 | +End If |
| 72 | +DateMaker = strDate |
| 73 | +End Function |
| 74 | +' ---------------------------------------------------------------------------- |
| 75 | +Private Function TimeMaker() As String |
| 76 | +'Writes the time in the following format (military time) HH:MM:SS |
| 77 | +Dim strTime As String |
| 78 | +If Hour(Now) < 10 Then |
| 79 | + strTime = "0" & Hour(Now) & ":" |
| 80 | +Else |
| 81 | + strTime = Hour(Now) & ":" |
| 82 | +End If |
| 83 | +If Minute(Now) < 10 Then |
| 84 | + strTime = strTime & "0" & Minute(Now) & ":" |
| 85 | +Else |
| 86 | + strTime = strTime & Minute(Now) & ":" |
| 87 | +End If |
| 88 | +If Second(Now) < 10 Then |
| 89 | + strTime = strTime & "0" & Second(Now) |
| 90 | +Else |
| 91 | + strTime = strTime & Second(Now) |
| 92 | +End If |
| 93 | +TimeMaker = strTime |
| 94 | +End Function |
| 95 | + |
0 commit comments