TheSwamp
Code Red => VB(A) => Topic started by: TimSpangler 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
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
Loop
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.
TIA
-
not much time so I hope I get this right... I use something like this....
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
Wend
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
replace(dataline,";",",",,,vbBinaryCompare)
and then use that separator to split your line into the array
dim LineArray as variant
LineArray =Split(dataline,",",,vbBinaryCompare)
-
Here is one way, pass each line to a function
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)
Next
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