B.Lowry
B.Lowry

Reputation: 1

VB code quit working

The following VBA worked this morning, but now I get an error

Input past end Of file

I read a file from server output. Search for status and write that to an output file.

Set objFSO = CreateObject("Scripting.FileSystemObject")

'How to read a file
'Open Input File
strFile = "C:\Users\vhaisplowryb\Desktop\TestFile.txt"
Set objFile1 = objFSO.OpenTextFile(strFile)

'Open Output File
outFile2 = "C:\Users\vhaisplowryb\Desktop\Processed Form Status.txt"
Set objFile2 = objFSO.CreateTextFile(outFile2,True)

Do Until objFile1.AtEndOfStream
  strLine = objFile1.ReadLine
  If InStr(strline, "FormatedConvertedBase64Value(Synced)") Then
    Found = 1
    Do Until Found = 2
      strLine = objFile1.ReadLine
      If InStr(strline, "<NS1:submissionId xmlns:")  Then
        objFile2.Write("*****************************************************************************************************")& vbCrLf 
        objFile2.Write strLine & vbCrLf
        Found = 2
        'Wscript.Echo strLine

        'Get form Status
        Found = 1
        Do Until Found = 2
          strLine = objFile1.ReadLine
          If InStr(strline, "Processed") Then
            objFile2.Write strLine & vbCrLf
            objFile2.Write("*****************************************************************************************************")& vbCrLf
            Found = 2
          ElseIf InStr(strline, "<NS1:status>") Then
            objFile2.Write strLine & vbCrLf
            'objFile2.Write("*****************************************************************************************************") & vbCrLf
            'Found = 2
          ElseIf InStr(strline, "<NS1:code>") Then
            objFile2.Write strLine & vbCrLf
            'objFile2.Write("*****************************************************************************************************") & vbCrLf
            'Found = 2
          ElseIf InStr(strline, "<NS1:value>") Then
            objFile2.Write strLine & vbCrLf
            objFile2.Write("*****************************************************************************************************")& vbCrLf
            Found = 2
          End If
        Loop
      End If
    Loop
  End If
Loop

objFile1.Close
objFile2.Close

Upvotes: 0

Views: 34

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200273

Your code has 3 loops nested within each other, and you read from the input file in each loop, but only the outermost loop checks for when the end of the file is reached.

Going out on a limb (since you elected to not disclose which statement is actually raising the error) I would suspect that the content of your input file is not what your code implicitly assumes. Hence the objFile1.ReadLine in line 17 or line 27 is trying to keep reading after the end of the file has been reached already, because none of your conditions match.

Change the condition of the nested loops from

Do Until Found = 2

to

Do Until Found = 2 Or objFile1.AtEndOfStream

and the problem will disappear.

Upvotes: 1

Related Questions