Christopher Greaves |
|||
Christopher Greaves |
|||
If you are interested in applications please visit www.VBASolutions.ca .
If you are interested in end-user macros please visit www.TorontoMacros.com .
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.
' 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
The date/time is separated from the message text by a tab character (ASCII 009) so that crude log file analysis can be performed.
' 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
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.
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