Hi Andy,
Sorry I have not responded sooner but I was off the grid for a while. Char(10) seems to work as you suggested and I finally got it working and tweeked the code just a bit to fit with the rest of the project. Even so I still have a few questions I hope you can help me with:
Here is the code as it stands right now:
Option Explicit
Sub RecodeText()
Dim strFileIn As String
Dim strFileOut As String
Dim lngUnitIn As Long
Dim lngUnitOut As Long
Dim strBuf As String
Dim strOut As String
Dim blnAppend As Boolean
strFileIn = Application.GetOpenFilename("Text Files,*.txt")
If strFileIn = "False" Then Exit Sub
strFileOut = Application.GetSaveAsFilename(InitialFilename:=strFileIn, FileFilter:="Text Files (*.txt), *.txt")
lngUnitIn = FreeFile
Open strFileIn For Input As lngUnitIn
lngUnitOut = FreeFile
Open strFileOut For Output As lngUnitOut
Do While Not EOF(lngUnitIn)
Line Input #lngUnitIn, strBuf
If Left(strBuf, 6) = "AB - " Then
strOut = ""
blnAppend = True
ElseIf Left(strBuf, 6) = "** - " Then
Print #lngUnitOut, strOut
blnAppend = False
End If
If blnAppend Then
' not yet
strOut = strOut & strBuf & Chr(10)
Else
Print #lngUnitOut, strBuf
End If
Loop
Close lngUnitIn
Close lngUnitOut
End Sub
The changes so far are minor as I just added Application.GetOpenFilename and Application.GetSaveAsFilename and changed the headers to represent what truly comes from the database. All the headers are a fixed length of six characters, spaces or numbers. In the example above I used "AB - " for the first header and "** - " for the second header. Is there any way for me to modify the code that I don't have to put down all the possibilities. The reason I ask this is because I noticed that the code will replace all the return carriages until it finds the stopping header. What I have found is that this stopping header (i.e. the next header) may be different from one file to the next.
The other question I have is when I try to save the new output file as the same name as the original file, it gives me error at this line of code:
Open strFileOut For Output As lngUnitOut
Is there anything I can do to allow me to overwrite the original file?
Thanks for all your help.
Abousetta
Bookmarks