How to read the txt files in VBA

Discussion in 'AutoCAD' started by freeflyair, Oct 10, 2004.

  1. freeflyair

    freeflyair Guest

    Hi all,
    Who does know how to read the txt files in the VBA?
    please tell me.
    Thanks in advance

    lily
     
    freeflyair, Oct 10, 2004
    #1
  2. Check out the FileSystemObject and the TextStream object in the MS Scripting
    Runtime library (SCRRUN, I think).
     
    John Goodfellow, Oct 10, 2004
    #2
  3. I wrote some code for someone a while ago
    that extracted data from the acad.pat file.

    Think the subject had something to do
    with. "Rotated HatchPattern".

    If you can't find it let me know...
     
    Paul Richardson, Oct 10, 2004
    #3
  4. Paul Richardson, Oct 10, 2004
    #4
  5. freeflyair

    John Coon Guest

    What do you want to do?
    just insert text from a file?
    insert points or blocks by coordinates from a file?

    John Coon

    This reads a station offset text file.
    Hope this helps

    Public Sub readInputFile()
    'if an error is encountered within this Subroutine, then it will
    automatically
    'continue at the line below labeled ErrControl:
    On Error GoTo ErrControl
    'set the proj variable to refer to the currently active project:
    Set proj = AeccApplication.ActiveProject
    'set the objAlign variable to refer to the currently selected alignment:
    Set objAlign = proj.Alignments.Item(proj.Alignments.CurrentAlignment)

    'If there is not a current alignment selected, then prompt user to select
    one
    'and continue to the line below labeled Exit_Here
    If (proj.Alignments.CurrentAlignment = Empty) Then
    MsgBox "Please set an alignment current before using this macro." &
    vbCrLf
    GoTo Exit_Here
    End If

    'define all local variables for this Subroutine (variables that are only
    accessible within this Subroutine):
    Dim dblSta As Double
    Dim dblOff As Double
    Dim dblDir As Double
    Dim dblEast As Double
    Dim dblNorth As Double
    Dim dblInsPnt(2) As Double
    Dim intInputFile As Integer
    'set the file number for the input file to the next available free file
    number
    'and then open the text file containing the station/offset data
    intInputFile = FreeFile()
    Open "d:\locations.txt" For Input As intInputFile
    Do While Not EOF(intInputFile)
    'read 2 sequential, delimited values from the input file and store them
    as sta & off
    Input #intInputFile, dblSta, dblOff

    'The following line reads input from dblSta and dblOff and converts
    'the values to northing, easting & direction (dblNorth, dblEast, dblDir)
    objAlign.PointLocation dblSta, dblOff, dblEast, dblNorth, dblDir

    'the following lines store the insertion point obtained above in
    'an array that can be used when calling the insertBlock subroutine
    dblInsPnt(0) = dblEast
    dblInsPnt(1) = dblNorth
    dblInsPnt(2) = 0

    'now call the insertBlock subroutine and feed it the insertion point
    Call insBlock(dblInsPnt)
    Loop

    Exit_Here:
    Exit Sub

    ErrControl:
    Call ErrorCentral(Err.Number)
    Resume Exit_Here

    End Sub

    Sub insBlock(varInsPnt As Variant)
    Dim objBlockRef As AcadBlockReference
    Dim blockName As String
    Dim xScale As Double
    Dim yScale As Double
    Dim zScale As Double
    Dim rot As Double

    blockName = "north"
    xScale = 1
    yScale = 1
    zScale = 1
    rot = 0

    Set objBlockRef = ThisDrawing.ModelSpace.insertBlock(varInsPnt,
    blockName, xScale, yScale, zScale, rot)

    End Sub


    Public Sub ErrorCentral(intErr As Double)
    Select Case intErr
    Case -2145320928
    'to handle escape key (I think?)
    Err.Clear

    Case Else
    'this handles any unkown error that is thrown here
    MsgBox "Unhandled error: " & Err.Description & vbCrLf & Err.Number
    Err.Clear
    End Select
    End Sub
     
    John Coon, Oct 10, 2004
    #5
  6. freeflyair

    freeflyair Guest

    I what to read a txt file to fill the database .
    I use autocad to draw pictures and want to fill some important data into
    Access datadbase to save.Firstly,I write the data in txt files in VisualLisp
    language .Secondly,to read the txt files in VBA and write in the database.
    I thingk this way to settle problem.
    Is there another way?
    Please tell me.
    Thank you .
     
    freeflyair, Oct 11, 2004
    #6
  7. freeflyair

    freeflyair Guest

    First,thank Paul richardson,John Coon.

    I does not find the file on www.AfraLisp.com
     
    freeflyair, Oct 11, 2004
    #7
  8. freeflyair

    Jürg Menzi Guest

    Hi freeflyair

    Why don't write the informations direct into the database?

    Cheers
     
    Jürg Menzi, Oct 11, 2004
    #8
  9. freeflyair

    TomD Guest

    I'll have to strongly second Jurg's suggestion. If you have the data
    structured in a CSV type format, then using ADO it would be relatively
    simple to put that into a database table. By the time you wrote the code to
    export to text, then import the text, you could learn enough ADO accomplish
    the same thing with much fewer steps.
     
    TomD, Oct 11, 2004
    #10
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.