Run-time error '91': Object variable or With block variable not set

JMongi

Active member
Local time
Yesterday, 23:58
Joined
Jan 6, 2021
Messages
802
I've checked the VBA MSoft files and can't figure out why this doesn't work. It's probably something basic...

Code:
Public Sub WriteErrorLog()
'Code uses enumeration due to late binding
Dim Log As String, LogPath As String
Dim fso As Object, LogFile As Object

Log = "ErrorLog.txt"
LogPath = "C:\KingslyOp\"

If FileExists(LogPath & Log) = False Then
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set LogFile = fso.CreateTextFile(LogPath & Log, True)
Else
    Set LogFile = fso.OpenTextFile(Log, 8, False, -2)
    'ForAppending = 8, TristateUseDefault = -2
End If

With LogFile
    .WriteBlankLines 1
    .Write "Test write"
    '.Write "Error " & Err.Number & " in " & ProcName & ": " & Err.Description
    .Close
End With
End Sub

It errors on Set LogFile = fso.OpenTextFile(Log, 8, False, -2)
I didn't explicitly test it, but the other Set LogFile used to create the file seemed to work as the log didn't originally exist before testing and now it does.
 
If it goes to the else the FSO is never set. Set the FSO before the If check.
 
You don't set the fso variable in the Else.
 
I told you it had to be a simple one...Thanks!
 
I got a question. The fileExists method as far as I know is a method of the FSO object. I do not understand how that can be called as you are doing. Is that a custom function? I would expect to see something like FSO.FileExists(filepath)
 
Custom function. I probably should rename it then.
 
I've got another probably easy question since we're doing the easy ones today ;)
When I was trying to troubleshoot, the F5, F8 actions weren't functioning as I thought they would. I read up on them, came back, and they did. Now, they aren't again.
Mainly, when I press F5 to run my subroutine to check if its working, I get a popup that lists "macros" and that's it. I figure there's something about the Editor environment I'm not understanding.
 
Well, I think maybe I figured this one out. It only lists Public Subs that have no parameters passed to them (even optional ones). In my troubleshooting I removed the parameter from the sub definition and this allowed me to run the sub. When the optional parameter is put back in, I get the popup. All the subs listed in the popup don't have parameters.
 

Users who are viewing this thread

Back
Top Bottom