juster21
Well-known Member
- Joined
- Jun 3, 2005
- Messages
- 867
Below is code that I use in a macro but one of the users gets a Disk Full error. I marked the line which throws the error. Any ideas on the cause or solution? Thanks!!
Code:
Sub ReplaceTextInFile(SourceFile As String, sText As String, rText As String) 'replaces with | in text file
Dim TargetFile As String, tLine As String, tString As String
Dim p As Integer, i As Long, F1 As Integer, F2 As Integer
TargetFile = "RESULT.TMP"
If Dir(SourceFile) = "" Then Exit Sub
If Dir(TargetFile) <> "" Then
On Error Resume Next
Kill TargetFile
On Error GoTo 0
If Dir(TargetFile) <> "" Then
MsgBox TargetFile & " already open, close and delete / rename the file and try again.", vbCritical
Exit Sub
End If
End If
F1 = FreeFile
Open SourceFile For Input As F1
F2 = FreeFile
Open TargetFile For Output As F2 '*****ERROR HERE*******
i = 1 ' line counter
Application.StatusBar = "Reading data from " & TargetFile & " ..."
While Not EOF(F1)
If i Mod 100 = 0 Then Application.StatusBar = "Reading line #" & i & " in " & TargetFile & " ..."
Line Input #F1, tLine
If sText <> "" Then
ReplaceTextInString tLine, sText, rText
End If
Print #F2, tLine
i = i + 1
Wend
Application.StatusBar = "Closing files ..."
Close F1
Close F2
Kill SourceFile ' delete original file
Name TargetFile As SourceFile ' rename temporary file
Application.StatusBar = False
End Sub