Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
So, I've been playing with the "Line Input" command to test
reading from text files. I am reading and displaying each paragraph with a MsgBox. If there are only 2 paragraphs in my input, why is a MsgBox appearing 4 times? It actually prints 2 empty Msgbox windows, so does that mean my input has extra CR or LF characters? Do I need to filter out blank lines? (P.S: I know Gary showed me a filter function, but that worked with a different type of variable.) Here is my sample text data: '--------------------begin "inp.txt"------------------------ Depart do be so he enough talent. Sociable formerly six but handsome. Up do view time they shot. He concluded disposing provision by questions as situation. Its estimating are motionless day sentiments end. Calling an imagine at forbade. At name no an what like spot. Pressed my by do affixed he studied. The him father parish looked has sooner. Attachment frequently gay terminated son. You greater nay use prudent placing. Passage to so distant behaved natural between do talking. Friends off her windows painful. Still gay event you being think nay for. In three if aware he point it. Effects warrant me by no on feeling settled resolve. '--------------------end "inp.txt"------------------------ Here is my code: Sub myImportTxt() Dim sFile As String Dim sLine As String Dim f As Integer f = FreeFile() sFile = "inp.txt" Open sFile For Input As #f Do Until EOF(f) Line Input #f, sLine MsgBox sLine ' MsgBox shows 4 times? Loop ' Why? How fix? Close #f End Sub |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
The structure of the text file will dictate what results you get! For
example, if you copy 2 paragraphs from a Word doc and paste them into a text editor (or a worksheet), you end up with 3 lines (or rows in wks), regardless of the number of sentences in each paragraph. A CrLf denotes a new line. Usually, paragraphs are separated by a blank line, ergo the 3 lines in the copy/paste example. So in this case the paragraph delimiter is a blank empty line. This complicates parsing the Word doc. When text files are properly structured to store data, the 1st line should contain fieldnames, the remaining lines are individual records, and there should be no blank lines anywhere in the file. If the Word doc above was structured to use paragraph spacing instead of a blank CrLf, the doc would parse more eaily because the paragraph separator will be a CrLf, same as the record separator in a properly structured data file. Go back to the Word doc an delete the empty line between the paragraphs, and set spacing before to render the desired 'appearance' of how the 2nd paragraph is spaced from the 1st. This structor would be dictated by Styles defs in a document template so the content conforms to some consistent standard. While positioning content via the space, tab, and enter keys for simple printing is the general practice, people get away with it because printing is a WYSIWYG process. Not the case when converting that doc to other file formats suitable for ereader devices because the rendered results are highly unpredictable. The template structure I described earlier renders predictable results in all output formats I've generated (11 to date). Note that output to a PDF file is not so much a conversion as it is a WYSIWYG printout. Probably more info here than needed, but importing text to a worksheet can be a very confusing process to one who doesn't understand file structure concepts! -- Garry Free usenet access at http://www.eternal-september.org Classic VB Users Regroup! comp.lang.basic.visual.misc microsoft.public.vb.general.discussion |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
"Robert Crandal" wrote:
Sub myImportTxt() Dim sFile As String Dim sLine As String Dim f As Integer f = FreeFile() sFile = "inp.txt" Open sFile For Input As #f Do Until EOF(f) Line Input #f, sLine MsgBox sLine ' MsgBox shows 4 times? Loop ' Why? How fix? Close #f End Sub The only time I see an "extra" iteration is when there is, indeed, an extra line at the end. You can guard against that with the following change to your loop: Line Input #f, sLine If Len(sLine) 0 Or Not EOF(f) Then MsgBox sLine End If You can guard against many empty lines at the end as follows: Dim b As Long b = 0 Do Until EOF(f) Line Input #f, sLine If Len(sLine) = 0 Then b = b + 1 Else For b = b To 1 Step -1 MsgBox Next MsgBox sLine End If Loop |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Il giorno sabato 14 marzo 2015 01:24:21 UTC+1, Robert Crandal ha scritto:
So, I've been playing with the "Line Input" command to test reading from text files. I am reading and displaying each paragraph with a MsgBox. If there are only 2 paragraphs in my input, why is a MsgBox appearing 4 times? It actually prints 2 empty Msgbox windows, so does that mean my input has extra CR or LF characters? Do I need to filter out blank lines? (P.S: I know Gary showed me a filter function, but that worked with a different type of variable.) Here is my sample text data: '--------------------begin "inp.txt"------------------------ Depart do be so he enough talent. Sociable formerly six but handsome. Up do view time they shot. He concluded disposing provision by questions as situation. Its estimating are motionless day sentiments end. Calling an imagine at forbade. At name no an what like spot. Pressed my by do affixed he studied. The him father parish looked has sooner. Attachment frequently gay terminated son. You greater nay use prudent placing. Passage to so distant behaved natural between do talking. Friends off her windows painful. Still gay event you being think nay for. In three if aware he point it. Effects warrant me by no on feeling settled resolve. '--------------------end "inp.txt"------------------------ Here is my code: Sub myImportTxt() Dim sFile As String Dim sLine As String Dim f As Integer f = FreeFile() sFile = "inp.txt" Open sFile For Input As #f Do Until EOF(f) Line Input #f, sLine MsgBox sLine ' MsgBox shows 4 times? Loop ' Why? How fix? Close #f End Sub Hi Robert, try: Public Sub Test() Const cstrPath = "D:\" Const cstrFile = "Line Input is reading extra lines.txt" Dim ff As Integer Dim s() As String Dim i As Long ff = FreeFile(0) Open cstrPath & cstrFile For Input Access Read As ff s = Split(Input$(LOF(ff), #ff), vbNewLine) Close Debug.Print "Items : "; UBound(s) + 1 For i = LBound(s) To UBound(s) If Len(s(i)) Then Debug.Print s(i) Next End Sub -- Ciao! Maurizio |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Extra Lines Inserted in Multi-Line Chart Titles | Charts and Charting in Excel | |||
To add extra line/lines to single cell in Excel spreadsheet | New Users to Excel | |||
How do I add an extra line to a line-column chart? | Charts and Charting in Excel | |||
line input problem-need to return 5 lines after a string is found | Excel Programming | |||
Excel 2000 Hanging while reading large file with Line Input | Excel Programming |