When the program runs, it goes through function Initialize
without issues. When function WriteString
is called, log's value has changed to "Nothing". On the Retry
, when Call Initialize
runs, Set log = FSO.OpenTextFile(sLogPath & sLogName, 2, 0)
returns Error 70.
Option Explicit
Public log As TextStream
Function Initialize()
Dim FSO As FileSystemObject
Dim sLogName As String
Dim sLogPath As String
Set FSO = New FileSystemObject
'Call Terminate
sLogPath = "C:\Users\" & UserName & "\Documents\VbaLog"
sLogName = "\progressLog.txt"
If Not FSO.FolderExists(sLogPath) Then
MkDir sLogPath
End If
Retry: On Error GoTo eError
If Dir(sLogPath & sLogName) <> "" Then
Set log = FSO.OpenTextFile(sLogPath & sLogName, 2, 0)
Else
Set log = FSO.CreateTextFile(sLogPath & sLogName, False)
End If
Exit Function
Set FSO = Nothing
Exit Function
eError:
If Err.Number = 70 Then
CreateObject("WScript.Shell").Run "attrib -r " & sLogPath
End If
GoTo Retry
End Function
Function WriteString(argument As String)
Retry:
If Not log Is Nothing Then
log.WriteLine Now & ": " & argument
Else
Call Initialize
GoTo Retry
End If
End Function
Function Terminate()
If Not log Is Nothing Then
log.Close
Else
Set log = Nothing
End If
End Function
The LogPath folder's property attribute is set to Read-Only. I uncheck that box but, it keeps coming back. I've even disabled inheritance for that folder and yet, nothing I do to the folder permantely uncheck the read-only attribute