Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Line wrapping in text files

Status
Not open for further replies.
Feb 25, 2008
46
US

I need to automatically wrap lines in a text file which exceed the column 80 mark to the next line. I receive text files which have more than a few lines which exceed this mark. Is there a code I can run on the text file so that it automatically scan the text file and wraps the text beyond the column 80 mark to the next line?
 
From your question, this doesn't seem like a VBA question. "Wrapping" implies how text is displayed, rather than how it is stored. Do you want to insert linefeed[/]s in lines of greater length than 80 characters? If so, I suggest any of many scripting languages (Tcl, Perl, Python; to name just 3). If instead, you want to ingest the text into some application and regulate the display, then VBA might be useful depending on which application. Which application?

_________________
Bob Rashkin
 


Bob,

Thanks for your response.

I am not looking at any particular application. Each line in the text file is supposed to display a record. Sometimes some lines carry more than one record and I have to do the wrapping manually as each of these text files are converted into table. There are no comma or tab separators I can use. I cannot use a space separator unless I can specify it has to be executed at the space at col 80.
I wanted a VBA code/macro or a batch file so that I could run it on the text files and it would wrap the data beyond col 80 to the next line.

The steps involved would be:
1.Opening the text file
2.Scanning it to find lines which have data beyond col 80
3.Wrapping that data to the next line
4.End of the procedure

I would prefer a batch file to do this.

Mark.
 
Code:
Sub Test()
   Const max_column As Long = 80
      
   Call Print_Lines(Application.ActiveWorkbook.Path & "\Input.txt", max_column)
End Sub

Private Sub Print_Lines(file_name As String, max_column As Long)
   Dim i As Long, curr_chars As String, curr_line As String, lines As String
   
   Open file_name For Input As #1
      Do While Not EOF(1)
         Line Input #1, curr_line
         lines = lines & curr_line
      Loop
   Close #1
   
   For i = 1 To Len(lines)
      curr_chars = curr_chars & Mid(lines, i, 1)
      
      If i Mod max_column = 0 Then
         Debug.Print curr_chars
         
         curr_chars = ""
      End If
   Next i
   
   Debug.Print curr_chars
End Sub
 




Use something like this...
Code:
   For i = 1 To Len(lines) Step 80
      curr_chars = Mid(lines, i, 80)
      
      Debug.Print curr_chars

   Next i


Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
another alternative;

Sub Test()
Call Print_Lines(Application.ActiveWorkbook.Path & "\Input.txt")
End Sub

Private Sub Print_Lines(file_name As String)

Dim mystring As String * 80
f As Integer

f = FreeFile
Open file_name For Random As f Len = Len(mystring)
Do While Not EOF(f)
Get f, , mystring
Debug.Print mystring
Loop
Close f

End Sub
 
I'd still use another scripting language so as not to have the overhead of Excel or Word hanging around.

_________________
Bob Rashkin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top