The MSDN website Logging Errors to a Text File offers the code below as an example of how to use the FileSystemObject.
Two questions (actually three):
1. Is the function GetTempDir a Windows API function and if so what dll file or other thing do I need to reference to use it.
2. I tried a “homemade” GetTempDir function and the code would never write the file. It would skip over the With txsStream part entirely. Can anyone tell mey what I'm doing wrong
Two questions (actually three):
1. Is the function GetTempDir a Windows API function and if so what dll file or other thing do I need to reference to use it.
2. I tried a “homemade” GetTempDir function and the code would never write the file. It would skip over the With txsStream part entirely. Can anyone tell mey what I'm doing wrong
Code:
Sub LogError(errX As ErrObject, _
Optional strProcName As String)
' This procedure logs errors to a text file. It is used in
' this chapter to log synchronization errors.
'
' Arguments:
' errX: A variable that refers to the VBA Err object.
Dim fsoSysObj As FileSystemObject
Dim filFile As File
Dim txsStream As TextStream
Dim lngErrNum As Long
Dim strPath As String
Dim strErrText As String
Set fsoSysObj = New FileSystemObject
' Store error information.
lngErrNum = errX.Number
strErrText = errX.Description
' Clear error.
errX.Clear
' Return Windows Temp folder.
strPath = GetTempDir
If Len(strPath) = 0 Then
GoTo LogError_End
End If
On Error Resume Next
' See if file already exists.
Set filFile = fsoSysObj.GetFile(strPath & APP_ERROR_LOG)
' If not, then create it.
If Err <> 0 Then
Set filFile = fsoSysObj.CreateTextFile(strPath & APP_ERROR_LOG)
End If
On Error GoTo 0
' Open file as text stream for reading.
Set txsStream = filFile.OpenAsTextStream(ForAppending)
' Write error information and close.
With txsStream
.WriteLine lngErrNum
.WriteLine strErrText
If Len(strProcName) > 0 Then .WriteLine strProcName
.WriteLine Now
.WriteBlankLines 1
.Close
End With
LogError_End:
Exit Sub
End Sub
Sub TestLogError()
' This procedure tests the LogError function.
On Error Resume Next
' Raise an error.
Err.Raise 11
' Log it.
LogError Err, "TestLogError"
End Sub