Barry,
Not sure what you mean by macro hangs?(infinite loop?)
Here is another method to create log files by Peter Hewett.
Here is code amended to work on both MacWord and WinWord.
I used Application.PathSeparator and changed the Kill() function to
KillLog() to avoid an endless loop in the class.
Insert a new class and name it LogIt then paste this Class code into it.
Peter Hewett's amended code:
****** Start of LogIt Class code *******
Private mstrFile As String
Private mstrPath As String
Private mboolEnabled As Boolean
Private mboolTimeStamp As Boolean
Public Sub Output(ByVal strText As String)
Const cProcedureName As String = "Log::Output"
Dim hFile As Long
On Error GoTo OutputError
' Everything must be right for us to log the passed text
If CanOutput Then
' Prefix log text with timestamp
If TimeStamp Then strText = Now & vbTab & strText
' Open log file for append
hFile = FreeFile
Open mstrPath & mstrFile For Append Access Write As hFile
' Write the output and tidy up
Print #hFile, strText
Close hFile
End If
OutputExit:
Exit Sub
OutputError:
mboolEnabled = False
Err.Raise Err.Number, cProcedureName, Err.Description
End Sub ' Output
Public Sub KillLog()
On Error GoTo HandleError
' Delete the current log file if it exists
If Len(Dir$(mstrPath & mstrFile)) > 0 Then
Kill mstrPath & mstrFile
End If
ExitHere:
Exit Sub
HandleError:
mboolEnabled = False
Err.Raise vbObjectError + 8001, "Log::Kill", _
Err.Description
Resume ExitHere
End Sub ' Reset
Public Property Get Path() As String
Path = mstrPath
End Property
Public Property Let Path(ByVal strPath As String)
strPath = Trim$(strPath)
If Right$(strPath, 1) <> Application.PathSeparator _
Then strPath = strPath & Application.PathSeparator
mstrPath = Trim$(strPath)
End Property
Public Property Get File() As String
File = mstrFile
End Property
Public Property Let File(ByVal FileName As String)
mstrFile = FileName
End Property
Public Property Get Enabled() As Boolean
Enabled = mboolEnabled
End Property
Public Property Let Enabled(ByVal EnableLogFile As Boolean)
mboolEnabled = EnableLogFile
End Property
Public Property Get TimeStamp() As Boolean
TimeStamp = mboolTimeStamp
End Property
Public Property Let TimeStamp(ByVal TimeStampOutput As Boolean)
mboolTimeStamp = TimeStampOutput
End Property
Public Property Get CanOutput() As Boolean
CanOutput = LenB(Path) > 0 And LenB(File) > 0 And Enabled
End Property
Private Sub Class_Initialize()
Me.Enabled = True
End Sub
****** End of LogIt Class code *******
Public Sub LogItTest()
Dim logFile As LogIt
' Create log file
Set logFile = New LogIt
logFile.Path = "Macintosh HD:Users:UserName
esktop:"
logFile.File = "DEBUG LogItTest.txt"
' Use it....
'logFile.KillLog 'To in effect, clear log, if desired.
logFile.TimeStamp = True
logFile.Output "This is the first log entry"
logFile.TimeStamp = False
logFile.Output "This is the second log entry "
Set logFile = Nothing
End Sub
Multiple logfiles could be going at the same time to log only certain parts
of code. You just need to create multiple instances of each Logit class.
Windows users could also use environmental variables:
logFile.Path = Environ("HOMEDRIVE") & Environ("HOMEPATH") & "\My Documents\"