My Technical Notes

Tuesday, 10 February 2015

OpenOffice: Simple File Logging

Because OpenOffice does not have an output window for logging/debugging statements, we have to roll our own solution.

Inspiration for the following `LoggModule` code was taken from the OO Forum.

Const PathLogg = "C:\Temp\OOoLog.txt"

' Add ClearLogg() Sub
Sub ClearLogg()
 Dim f1 As Integer
 f1 = FreeFile()
 Open PathLogg For Output As #f1
 Close #f1
 ' clear the entire log by deleting all the content
End Sub

' Add DeleteLogg(), 
Sub DeleteLogg()
 If FileExists(PathLogg) Then
  Kill PathLogg
 End If
 ' delete the log file completely.
End Sub

Sub Logg(msg As String) 
 ' log the string to the log file.
 Dim f1 As Integer
 f1 = FreeFile()
    Open PathLogg For Append As #f1
    print #f1, Now & " : " & msg
    Close #f1
End Sub
  • `PathLogg` is the path of the log file. This constant is used in all of the module's subs
  • `Logg` logs a message to the `PathLogg` file.
  • `ClearLogg` keeps the log file, but clears its contents.
  • `DeleteLogg` deletes the log file.

To view the log file, one solution is to use Notepad++ and the "Document Monitor" plugin.

No comments: