Christopher Greaves

Contact Me

ON LINE STORE

ON LINE STORE

ON LINE STORE

ON LINE STORE

Building and Maintaining Excel Libraries

If you are interested in applications please visit www.VBASolutions.ca .

If you are interested in end-user macros please visit www.TorontoMacros.com .

Logging

Three levels of logging functions are provided: PrintFile, LogFile and AMLog.

PrintFile

PrintFile takes two string arguments – a file name and a string of text to be written to the file.

This is the basic write-string-to-file function.

If no extent or path is specified, they will be provided. The default extent is TXT and the default path is wherever the utility library happens to be stored.

Public Function PrintFile(strFile As String, strMsg As String)

' Procedure : PrintFile

' Description: Print a message to a file.

' Copyright: Christopher Greaves

' Inputs: The local or full file name, a string.

' Returns: None.

' Assumes: None.

' Side Effects: None.

' Tested: By the calls shown below.

Dim intFile As Integer

intFile = FreeFile

Dim strWorkFile As String

strWorkFile = strFile

' add an extent if there is not one yet.

If InStr(1, strFile, strcExtentSeparatorDefaultValue) > 0 Then

Else

strWorkFile = strWorkFile & strcExtentTxt

End If

' add a path if there is not one yet.

If InStr(1, strFile, Application.PathSeparator) > 0 Then

Else

strWorkFile = strMacroContainerPathValue & strWorkFile

End If

Open strWorkFile For Append As #intFile

Print #intFile, strMsg

Close #intFile

End Function

LogFile

LogFile adds a date and time stamp to the message text before sending it off to PrintFile.

The date/time is separated from the message text by a tab character (ASCII 009) so that crude log file analysis can be performed.

Public Function LogFile(strFile As String, strMsg As String)

' Procedure : LogFile

' Description: Time stamp a message string to a file.

' Copyright: Christopher Greaves

' Inputs: The local or full file name, a string.

' Returns: None.

' Assumes: None.

' Side Effects: None.

' Tested: By the calls shown below.

Call PrintFile(strFile, strDateTime & vbTab & strMsg)

'Sub TESTLogFile()

' Call LogFile("erase.txt", "Here is a first message") ' stored in macroContainer folder

' Call LogFile("c:\erase.txt", "Here is a second message") ' stored in boot root folder

' Call LogFile(MacroContainer.Path & Application.PathSeparator & "erase.txt", "Here is a third message")

'End Sub

End Function

AMLog

AMLog Uses a document to identify the path and document name of the logging file.

My practice is to name application templates with five alphabetic characters followed by a three-digit version number, thus “Water153.dot”.

AMLog uses the document name to develop a path and file name, in the example above, perhaps “C:\Documents and Settings\ChrisL\Application Data\Greaves\Water\Water.LOG”.

If the log file exceeds two megabytes in size, it is trimmed to 50% its size by removing the stalest entries from the front of the file.

The augmented message is always written to the log file.

The augmented message can be delivered to a pop-up message box.

The augmented message can be issued to VBE’s Immediate Window.

Thus an event is always logged to the hard drive (and the log file closed!) before we offer the user information.

Public Function AMLog(doc As Document, strMsg As String, Optional blnMsgBox, Optional blnDebug)

Dim strMsgAugmented As String

strMsgAugmented = strMsg

strMsgAugmented = Environ("username") & vbTab & strMsgAugmented

strMsgAugmented = strApplication(doc) & vbTab & strMsgAugmented

strMsgAugmented = strDateTime & vbTab & strMsgAugmented

Dim strPrintFileName As String

strPrintFileName = strEnvironmentPath(doc) & strApplication(doc) & strcLogFileExtent

If UW.blnFileExists(strPrintFileName) Then

Call ReduceFileSize(strPrintFileName, 2000000, 0.5)

Else

End If

Call PrintFile(strPrintFileName, strMsgAugmented)

If IsMissing(blnMsgBox) Then

Else

If blnMsgBox Then

MsgBox strMsg

Else

End If

End If

If IsMissing(blnDebug) Then

Else

If blnDebug Then

Debug.Print strMsgAugmented

Else

End If

End If

End Function

Contact Me