Read info from a text file

Discussion in 'AutoCAD' started by Matthew.Corbin, Mar 3, 2004.

  1. I would like to read a paragraph from a text file and insert it into CAD as mtext. I understand the concepts of reading from the file, I have that working fine. What I can't figure out is how to combine all the lines into one string. At the moment i've got this code working, but it isn't quite doing what I want it to. It is putting the text where I want it, but only putting the last line read as the contents. Where am I going wrong?

    Open FileName For Input Access Read As #File
    Do While Not EOF(File)
    Line Input #File, TextFromFile
    Loop
    Close #File
    ThisDrawing.ModelSpace.AddMText InsPnt,500,TextFromFile

    I'm sure I have to put code in the loop that combines all the text lines into one. Hope someone can shed some light.
    Thanks for all the help, This NG has been a huge help.

    Kind Regards,
    Matthew Corbin
     
    Matthew.Corbin, Mar 3, 2004
    #1
  2. Matthew.Corbin

    Bill Wright Guest

    Add a String holder

    Dim FileContents As String

    Open FileName For Input Access Read As #File
    Do While Not EOF(File)
    Line Input #File, TextFromFile

    '**** add the line to the rest of the string
    FileContents = FileContents & TextFromFile

    '****or you may have to add a return too
    FileContents = FileContents & vbCr & TextFromFile
    '***

    Loop
    Close #File

    '**** now use the file contents string for your mtext
    ThisDrawing.ModelSpace.AddMText InsPnt,500,FileContents


    good luck
     
    Bill Wright, Mar 3, 2004
    #2
  3. Matthew.Corbin

    Warren M Guest

    Hello Matthew.

    You need to setup a variable to hold the 'final' string result, and keep appending to that string as you loop through the file.

    Something like:
    Dim strFinalText as String
    Open FileName For Input Access Read As #File
    Do While Not EOF(File)
    Line Input #File, TextFromFile
    strFinalText = strFinalText & TextFromFile
    Loop
    Close #File
    ThisDrawing.ModelSpace.AddMText InsPnt,500,strFinalText

    Hope this helps
    Warren Medernach
     
    Warren M, Mar 3, 2004
    #3
  4. Matthew.Corbin

    Bill Wright Guest

    Yep!
     
    Bill Wright, Mar 3, 2004
    #4
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.