1

I am getting the error "Object variable not set" on line 15 when I run my code. I do not get it every time. It seems to be random. I have searched, but cannot find any reason for this. I was having an issue with the variable not getting cleared all the time, so I just added line 42 Set strText = Nothing. My issue with the variable not setting properly went away, but now I have this. Any help would be greatly appreciated. Code is below.

Option Explicit
Dim i, objFSO, objFile, strText, Fields, TimeStamp, Location, MachNum, Amount, Theme, objSMFile, SMLine, p, CSVLine, objReportFile, FileName
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Do while i<>1
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    If objFSO.FileExists("C:\Print\output.txt") Then
        '*** Clean up CR/LF and Make CSV Variable ***
        Set objFile = objFSO.OpenTextFile("C:\Print\output.txt", ForReading)
        If Not objFile.AtEndOfStream Then strText = objFile.ReadAll
        objFile.Close
'       WScript.Sleep 1000
        objFSO.CopyFile "C:\Print\output.txt", "C:\Print\outputCopy.txt"
        objFSO.DeleteFile("C:\Print\output.txt")
'       WScript.Sleep 3000
        strText = Replace(strText, vbCrlf, "")
        strText = Replace(strText, "   ", ";")
        ' Split by semi-colon into an array...
        Fields = Split(strText, ";")
        TimeStamp = Fields(0)
        Location = Fields(1)
        MachNum = Fields(3)
        Amount = Fields(4)
        '*** Find Machine Number in Slot Master ***
        Set objSMFile = objFSO.OpenTextFile("C:\Scripts\SlotMaster.csv", ForReading)
        do while not objSMFile.AtEndOfStream
            SMLine=objSMFile.readline
            p=instr(SMLine, MachNum)
            if p>0 then CSVLine = SMLine
        Loop
        ' Split by comma into an array...
        Fields = Split(CSVLine, ",")
        Theme = Fields(7)
        '*** Create Tab Delimited Text File ***
        FileName = Year(Now) & "-" & Month(Now) & "-" & Day(Now) & " Jackpots.txt"
        If Not objFSO.FileExists("C:\Print\" & FileName) Then
            Set objReportFile = objFSO.CreateTextFile("C:\Print\" &FileName)
            objReportFile.Close
        End If
        Set objReportFile = objFSO.OpenTextFile("C:\Print\" & FileName, ForAppending)
        objReportFile.Write TimeStamp & vbTab & Location & vbTab & Amount & vbTab & Theme & vbCrLf
        objReportFile.Close
        Set strText = Nothing
    End If
Loop
1

1 Answer 1

8

SHORT ANSWER

strText is not an object. Its there to handle a string.

You should thus reinit it with:

strText= ""

LONG ANSWER

When you do

Set strText = Nothing
  1. You implicitly declare strText as an object, and thus it is not a string anymore. Trying to assign it a value "" (blank string) is therefore not allowed.
  2. You decrement it's reference counter, which renders the object not addressable anymore and candidate for the garbage collector. So the next time Replace() or .ReadAll would be executed on it, it would fail because it just does not exists anymore. And even if it existed it would fail because it is not a string anymore, as stated previously.
Sign up to request clarification or add additional context in comments.

1 Comment

Now I am getting a random "Subscript out of range: '[number: 0]" on line 17.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.