I am trying to read a ".txt" file and append to the end of the file to create an on going list. I have found some examples from previous issues on this forum but I am have trouble with my output in the text file not exiting the loop. I am guessing that the problem is with the "Do Until sdr.AtEndOfStream" line. Can someone explain what I am doing wrong, and where could I reference something to fix the problem. Thank you in advance.
Code:
Public Sub WriteFile(strMapNumber, str200Map)
Dim FileName As String
FileName = "C:\Temp\Duplicate_OH.txt"
If Dir(FileName) <> "" Then
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim fso As Object
Dim fdw As Object, sdw As Object
Dim fdr As Object, sdr As Object
Dim strText As String
'create file sysytem object
Set fso = CreateObject("Scripting.FileSystemObject")
'get file to read and open text stream
Set fdr = fso.GetFile(FileName)
Set sdr = fdr.OpenAsTextStream(ForReading, TristateUseDefault)
'create new file to write to
fso.CreateTextFile (FileName)
Set fdw = fso.GetFile(FileName)
Set sdw = fdw.OpenAsTextStream(ForAppending, TristateUseDefault)
'iterate through the end of first file
Do Until sdr.AtEndOfStream
'read string from the first file
strText = sdr.ReadLine & vbNewLine '<--added carriage return to jump on the next line
'write to the second one
sdw.write strText & "100 Scale: " & strMapNumber & vbTab & "200 Scale: " & str200Map & vbNewLine
Loop
'close both files and clean up
sdr.Close
sdw.Close
Set sdr = Nothing
Set sdw = Nothing
Set fdr = Nothing
Set fdw = Nothing
Set fso = Nothing
Else
Set fso = CreateObject("Scripting.FileSystemObject")
Set fdw = fso.CreateTextFile("C:\Temp\Duplicate_OH.txt", True)
fdw.writeline ("This Is A Test1.")
fdw.Close
End If
End Sub