Exporting text from AutoCAD to Excel

Discussion in 'AutoCAD' started by Lynne, Jul 7, 2004.

  1. Lynne

    Lynne Guest

    I can only find a way of taking text from AutoCAD to Excel by picking on each piece of text, which is quite tedious.
    Is their a way of getting visual basic to find the text by its co-ordinates?
     
    Lynne, Jul 7, 2004
    #1
    1. Advertisements

  2. Lynne

    Matt W Guest

    You can do something like this...

    Public Sub TextToExcel()
    Dim objText As AcadText

    For Each objText In ThisDrawing.ModelSpace
    ' Insert code to export to Excel here...
    Next objText
    End Sub

    If you need more help, let me know...

    --
    Matt W

    The difference between genius and stupidity is that genius has its limits.
    | I can only find a way of taking text from AutoCAD to Excel by picking on
    each piece of text, which is quite tedious.
    | Is their a way of getting visual basic to find the text by its
    co-ordinates?
     
    Matt W, Jul 7, 2004
    #2
    1. Advertisements

  3. Matt,
    Your code would crash if there were any non-text items in modelspace. It
    needs to be something more like:

    dim objEnt as AcadEntity
    If TypeOf objEnt is AcadText Then
    Set objText = objEnt
    Lynne,
    You could use the SelectOnScreen method, with a filter on so that only text
    items are selected, to get all the text in an area. Or, to use Matt's code,
    you would check the .InsertionPoint of each Text item to see if it's the
    one(s) you want.


    HTH,
    James
     
    James Belshan, Jul 7, 2004
    #3
  4. Lynne

    Matt W Guest

    Yeah, you're right.
    I just threw something together really quick, which is why it worked on a
    BLANK drawing with only 3 pieces of text. :)

    --
    Matt W

    The difference between genius and stupidity is that genius has its limits.
    | Matt,
    | Your code would crash if there were any non-text items in modelspace. It
    | needs to be something more like:
    |
    | dim objEnt as AcadEntity
    | > Dim objText As AcadText
    | >
    | > For Each objEnt In ThisDrawing.ModelSpace
    | If TypeOf objEnt is AcadText Then
    | Set objText = objEnt
    | > ' Insert code to export to Excel here...
    | EndIf
    | > Next objEnt
    |
    | Lynne,
    | You could use the SelectOnScreen method, with a filter on so that only
    text
    | items are selected, to get all the text in an area. Or, to use Matt's
    code,
    | you would check the .InsertionPoint of each Text item to see if it's the
    | one(s) you want.
    |
    |
    | HTH,
    | James
    |
    |
     
    Matt W, Jul 7, 2004
    #4
  5. Lynne

    Matt W Guest

    Lynne:

    You can use this to import text from AutoCAD to Excel as well as the
    coordinates...
    You'll just have to add a reference to Excel in the VBA IDE.

    Code:
    Option Explicit
    
    Public intRow As Integer
    Public strCell As String
    
    ' Application - Excel
    Public oExcel As Excel.Application
    Public oBook As Excel.Workbook
    Public oSheet As Excel.Worksheet
    
    
    Private Sub CreateExcel()
    Dim i As Integer
    Set oExcel = CreateObject("Excel.Application")
    Set oBook = oExcel.Workbooks.Add
    Set oSheet = oBook.Worksheets(1)
    
    oExcel.Visible = True
    End Sub
    
    Public Sub TextToExcel()
    Dim obj As AcadEntity
    Dim objText As AcadText
    Dim varInsert As Variant
    
    ' Create an Excel object
    CreateExcel
    oExcel.Cells(1, 1).Value = "TEXT"
    oExcel.Cells(1, 2).Value = "X: COORDINATE"
    oExcel.Cells(1, 3).Value = "Y: COORDINATE"
    oExcel.Cells(1, 4).Value = "Z: COORDINATE"
    
    intRow = 2
    
    For Each obj In ThisDrawing.ModelSpace
    If TypeOf obj Is AcadText Then
    Set objText = obj
    oExcel.Cells(intRow, 1).Value = objText.TextString
    varInsert = objText.InsertionPoint
    oExcel.Cells(intRow, 2).Value = varInsert(0)
    oExcel.Cells(intRow, 3).Value = varInsert(1)
    oExcel.Cells(intRow, 4).Value = varInsert(2)
    intRow = intRow + 1
    End If
    Next obj
    End Sub
    
    --
    Matt W

    The difference between genius and stupidity is that genius has its limits.
    | Yeah, you're right.
    | I just threw something together really quick, which is why it worked on a
    | BLANK drawing with only 3 pieces of text. :)
    |
    | --
    | Matt W
    |
    | The difference between genius and stupidity is that genius has its limits.
    | | | Matt,
    | | Your code would crash if there were any non-text items in modelspace.
    It
    | | needs to be something more like:
    | |
    | | dim objEnt as AcadEntity
    | | > Dim objText As AcadText
    | | >
    | | > For Each objEnt In ThisDrawing.ModelSpace
    | | If TypeOf objEnt is AcadText Then
    | | Set objText = objEnt
    | | > ' Insert code to export to Excel here...
    | | EndIf
    | | > Next objEnt
    | |
    | | Lynne,
    | | You could use the SelectOnScreen method, with a filter on so that only
    | text
    | | items are selected, to get all the text in an area. Or, to use Matt's
    | code,
    | | you would check the .InsertionPoint of each Text item to see if it's the
    | | one(s) you want.
    | |
    | |
    | | HTH,
    | | James
    | |
    | |
    |
    |
     
    Matt W, Jul 7, 2004
    #5
  6. Lynne

    Lynne Guest

    Thanks James and Matt for your assistance.
    Could you help me to get the Autocad drawing name from autocad?
     
    Lynne, Jul 21, 2004
    #6
  7. Lynne

    MP Guest

    system variable "Dwgname", also of interest "Dwgprefix"
    ???
     
    MP, Jul 21, 2004
    #7
  8. Lynne

    Jürg Menzi Guest

    Hi Lynne

    Thisdrawing.GetVariable("DWGNAME")
    Thisdrawing.GetVariable("DWGPREFIX")

    Cheers
     
    Jürg Menzi, Jul 21, 2004
    #8
  9. Lynne

    hengyouleang

    Joined:
    Dec 27, 2015
    Messages:
    1
    Likes Received:
    0
    Hello Mr. Matt W
    how can i used this code?
     
    hengyouleang, Dec 27, 2015
    #9
    1. Advertisements

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.