Import and split long text into bits of 80 charters

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I’m quit new at this, so I could really need some help, to make some VBA code
for my problem.
I have a text file with all of the text in one single line, but I need it to
be in lines of 80 charters, and maybe save it to a new text file.
From there it isn’t such a hard task to get the information out of it, I
just have to import it as a fixed with file.

I really hope somebody can help me, then until now I have made the first
part by hand.
Thanks in advanced.
 
Leika said:
I'm quit new at this, so I could really need some help, to make some
VBA code for my problem.
I have a text file with all of the text in one single line, but I
need it to be in lines of 80 charters, and maybe save it to a new
text file.
From there it isn't such a hard task to get the information out of
it, I just have to import it as a fixed with file.

I really hope somebody can help me, then until now I have made the
first part by hand.
Thanks in advanced.

Here's some sample code:

'----- start of code -----
Sub MakeFile80()

Dim lngBytesLeft As Long
Dim iFileIn As Integer
Dim iFileOut As Integer
Dim strLine As String

iFileIn = FreeFile
Open "C:\Temp\OneLongLine.txt" For Input As iFileIn

iFileOut = FreeFile
Open "C:\Temp\Fixed80Lines.txt" For Output As iFileOut

lngBytesLeft = LOF(iFileIn)

Do While lngBytesLeft > 0

If lngBytesLeft < 80 Then
strLine = Input(lngBytesLeft, iFileIn)
' Drop trailing CR/LF if present.
If Right(strLine, 2) = vbCrLf Then
strLine = Left(strLine, Len(strLine) - 2)
End If
' Pad short line to 80 bytes.
strLine = Left(strLine & Space(80), 80)
lngBytesLeft = 0
Else
strLine = Input(80, iFileIn)
lngBytesLeft = lngBytesLeft - 80
End If

Print #iFileOut, strLine

Loop

Close iFileIn, iFileOut

End Sub
'----- end of code -----
 
Thanks.
It was a super answer, and it worked perfect.
Now I can get on with my project.


"Dirk Goldgar" skrev:
 
Back
Top