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

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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