Author Topic: Reading from a text file?  (Read 1207 times)

0 Members and 1 Guest are viewing this topic.


  • Water Moccasin
  • Posts: 2010
  • CAD Naked!!
Reading from a text file?
« on: July 19, 2006, 11:15:48 AM »
Ok i'm back (still playing around getting used to it)

I have a question about reading from a text file.

I have a text file with lines like this:
A-ANNO-DIMS;Witness/extension lines, dimension terminators, dimension text;Continuous;35;7;1;LayerCreator;

(all one line no wrap)

I also have this code
Code: [Select]
Public Sub ReadFile()
Dim LayerLine As String
  Open "C:\Acad ToolBOX\Layer Creator\Layer Files\Architectural.lyr" For Input As #1    'Open file for input
  Do While Not EOF(1)                           'Loop until end of file
    Input #1, LayerLine           'Read data into two variables
    Debug.Print LayerLine       'Print data to the Immediate window
  Close #1                                                'Close file
End Sub

Problem is when I run it it craps out on the comma's  how can I get it to

a. read the line as one line

b. read the line and seperate it at the semicolons?
- This would be the best option then I could set variables to each one (would list be Arrays?)

Anyway this is  from my layercreator program that I wrote, I may try to port it to vba.

ACA 2015 - Windows 7 Pro
All Comments and Content by TimSpangler, Copyright 2016


  • Guest
Re: Reading from a text file?
« Reply #1 on: July 19, 2006, 11:44:08 AM »
not much time so I hope I get this right... I use something like this....

Code: [Select]
Public Sub ReadMyFile()

    Dim inputfile As String
        inputfile = "C:\myfile.txt"
    Dim DataFile As Integer
        DataFile = FreeFile
    Dim Dataline As String
    Open inputfile For Input As #DataFile
        While Not EOF(DataFile)
            Line Input #DataFile, Dataline

                ' now you've got the line so do your stuff here

    Close #DataFile
end sub

you may want to look at the replace method to replace the commas in your line with semi colons, or vice versa. Then you can use the split function to return an array from your line

this will replace semi-colons with commas
Code: [Select]
replace(dataline,";",",",,,vbBinaryCompare)and then use that separator to split your line into the array
Code: [Select]
dim LineArray as variant
LineArray =Split(dataline,",",,vbBinaryCompare)


  • Water Moccasin
  • Posts: 1851
Re: Reading from a text file?
« Reply #2 on: July 20, 2006, 01:15:13 AM »

Here is one way, pass each line to a function

Code: [Select]
Sub TestParse()
    Dim sLine As String
    Dim Thang
    sLine = "A-ANNO-DIMS;Witness/extension lines, dimension terminators, dimension text;Continuous;35;7;1;LayerCreator;"
    Thang = ParseLayerInfo(sLine)
    For i = 0 To UBound(Thang)
        Debug.Print Thang(i)
End Sub

Public Function ParseLayerInfo(sLine As String) As Variant
    Dim Pos1 As Long, Pos2 As Long
    Dim i As Integer
    Dim vInfo(6) As String
    Pos1 = InStr(1, sLine, ";")
    vInfo(0) = Left(sLine, Pos1 - 1)
    For i = 1 To 6
        Pos2 = InStr(Pos1 + 1, sLine, ";")
        vInfo(i) = Mid(sLine, Pos1 + 1, (Pos2 - Pos1) - 1)
        Pos1 = Pos2
    Next i
    ParseLayerInfo = vInfo
End Function