File System Object

sassriverrat

Well-known Member
Joined
Oct 4, 2018
Messages
655
Good Morning,

I'm working on an error handler and stumbled upon this wonderful section of code, available from https://bettersolutions.com/vba/error-handling/log-file.htm

Anyway, I don't want to enable File System Object in my reference library so I tried modifying some script to this
Rich (BB code):
Dim g_objFSO As ObjectDim g_scrText As Object
Set g_objFSO = CreateObject("Scripting.FileSystemObject")
Set g_scrText = CreateObject("Scripting.TextStream")
and it hasn't worked. I was hoping someone might have an appropriate way to fix this. Below is the total compilation of the code. All of it has been placed in it's own module in VBA and then the "Call Error_Handle" piece in the code of my stuff (see below as well). Maybe I've done something else wrong here. Thanks!

Total Error Coding
Rich (BB code):
'added to the top of a section of my code
Const sProcName As String = "Instructional"
On Error GoTo Helper

'taken from mine
'Error Clearing Code
Exit Sub
Helper:
    resp = MsgBox("We're sorry to see you've encountered an error." & vbCrLf & vbCrLf & "To proceed, we recommend you contact the Developer " & _
    "with error codes [1126] and " & "[" & Err.Number & "-" & Err.Description & "]." & vbCrLf & vbCrLf & "To attempt to patch your problem at least " & _
    "temporarily, we recommend you click [Yes] to see help directions. Would you like to continue?", vbYesNoCancel, name)
        If resp = vbYes Then
            UserForm18.Show
            Call Error_Handle(sProcName, Err.Number, Err.Description)
            'Dim ermsg As String
            'ermsg = Environ("Userprofile") & "" & Sheets("Developer").Range("E44") & "" & "Error Msg " & Format(Date, mm - dd - yyyy) & ".txt"
            'Open ermsg For Output As #1 
            'Write #1 , "error codes [1126] and " & "[" & Err.Number & "-" & Err.Description
            'Close #1 
            
        ElseIf resp = vbNo Then
            Exit Sub
        ElseIf resp = vbCancel Then
            Exit Sub
        End If

'now here-on down goes in its own module I believe (see website)
Public Sub Error_Handle(ByVal sRoutineName As String, _ 
                         ByVal sErrorNo As String, _ 
                         ByVal sErrorDescription As String) 
Dim sMessage As String 
   sMessage = sErrorNo & " - " & sErrorDescription 
   Call MsgBox(sMessage, vbCritical, sRoutineName & " - Error") 
   Call LogFile_WriteError(sRoutineName, sMessage) 
End Sub 


Public Function LogFile_WriteError(ByVal sRoutineName As String, _
                             ByVal sMessage As String)


Dim g_objFSO As Object
Dim g_scrText As Object
Set g_objFSO = CreateObject("Scripting.FileSystemObject")
Set g_scrText = CreateObject("Scripting.TextStream")
Dim sText As String
   On Error GoTo ErrorHandler
   
   If (g_scrText Is Nothing) Then
      If (g_objFSO.FileExists("C:\temp\mylog.txt") = False) Then
         Set g_scrText = g_objFSO.OpenTextFile("C:\temp\mylog.txt", IOMode.ForWriting, True)
      Else
         Set g_scrText = g_objFSO.OpenTextFile("C:\temp\mylog.txt", IOMode.ForAppending)
      End If
   End If
   sText = sText & "" & vbCrLf
   sText = sText & Format(Date, "dd MMM yyyy") & "-" & Time() & vbCrLf
   sText = sText & " " & sRoutineName & vbCrLf
   sText = sText & " " & sMessage & vbCrLf
   g_scrText.WriteLine sText
   g_scrText.Close
   Set g_scrText = Nothing
   Exit Function
ErrorHandler:
   Set g_scrText = Nothing
   Call MsgBox("Unable to write to log file", vbCritical, "LogFile_WriteError")
End Function


'These were from the website but I tried tweaking the previous function to add these in...maybe that was the mistake

Public g_objFSO As Scripting.FileSystemObject 
Public g_scrText As Scripting.TextStream 

        
End Sub
[COLOR=[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1"]#1[/URL] 24080]



[/COLOR]
 
Last edited:

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
I doubt that will be possible while code is running.
 
Upvote 0
Hmmm...because the code I have will give the name of the module running as well as its error in a notepad.txt file..... see code below.


Code:
  Public Sub Error_Handle(ByVal sRoutineName As String, _                         ByVal sErrorNo As String, _
                         ByVal sErrorDescription As String)
Dim sMessage As String
   sMessage = sErrorNo & " - " & sErrorDescription
   'Call MsgBox(sMessage, vbCritical, sRoutineName & " - Error")
   With UserForm18
        .Label4.Caption = sRoutineName & " [" & sMessage & "]"
        .Show
    End With
   Call LogFile_WriteError(sRoutineName, sMessage)
End Sub






Public Function LogFile_WriteError(ByVal sRoutineName As String, _
                             ByVal sMessage As String)


Dim g_objFSO As Object
Dim g_scrText As Object
Set g_objFSO = CreateObject("Scripting.FileSystemObject")
Dim sText As String
Dim errdrive As String
Dim erraddress As String
errdrive = Sheets("Developer").Range("E44")
erraddress = Sheets("Developer").Range("J44")
Dim name As String
    name = Sheets("Notes").Range("N4")


errfile = errdrive & "\" & erraddress & ".txt"
   'On Error GoTo ErrorHandler
   
   If (g_scrText Is Nothing) Then
      If (g_objFSO.FileExists(errfile) = False) Then
         Set g_scrText = g_objFSO.OpenTextFile(errfile, 2, True)
      Else
         Set g_scrText = g_objFSO.OpenTextFile(errfile, 8)
      End If
   End If
   sText = sText & "" & vbCrLf
   sText = sText & Format(Date, "dd MMM yyyy") & "-" & Time() & vbCrLf
   sText = sText & " " & sRoutineName & vbCrLf
   sText = sText & " " & sMessage & vbCrLf
   g_scrText.WriteLine sText
   g_scrText.Close
   Set g_scrText = Nothing
   Exit Function
'ErrorHandler:
   Set g_scrText = Nothing
   Call MsgBox("Unable to write to log file", vbCritical, name)
End Function
 
Upvote 0
Yes, you can do that but you can't access the actual code programmaticly which it appears to be what you want to do.

Why do you want to copy the procedure anyway?
 
Upvote 0
So how would I do it?

My thought is that I'd like to have the exact procedure saved in a text file so that if there's an error- the file can be printed or emailed and printed and checked line by line to see what produced the error.
 
Upvote 0
You want to record/send an entire procedure whenever there's an error?
 
Upvote 0
Yes- and I'm able to record the name of the procedure and send it currently but I want to include the procedure itself, not just the name.
 
Upvote 0
The only way I can see that being possible would be if you were to write out the procedures to text files and then somehow read the appropriate text file and then write it to the log.

That seems like overkill to me - why do you need to see the entire code when you know the procedure name, know where the error occurred etc.

Also, what if there are multiple errors in the same procedure?

Will you write the entire procedure to the log file every time?

If you do then there's a good change you'll end up with pretty big log files with a lot of, as far as I can see, unnecessary data.

That unnecessary data could even make it harder to trace errors/debug etc.
 
Upvote 0
well this workbook is being dispersed to about 130 machines and i like working out errors (somedays) line by line on paper. Yes, they could certainly become long log files...but not sure of a better way to do it.
 
Upvote 0
Don't you have the code stored in a central location?

If you do can't you go through the log file and locate the relevant code where it's actually stored rather than in a text file.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top