Get value block attributes from AutoCAD into excel with VBA.

Get value block attributes from AutoCAD into excel with VBA.

tonythm
Advocate Advocate
6,917 Views
1 Reply
Message 1 of 2

Get value block attributes from AutoCAD into excel with VBA.

tonythm
Advocate
Advocate

Hello Everyone,

 

Please help me!

I have AutoCAD drawing with 2 block, each in block have 6 attributes. I use excel VBA to get value attribute. How to do get value in each attributes that not select block (it mean automatic get value into excel)

 

0 Likes
6,918 Views
1 Reply
Reply (1)
Message 2 of 2

grobnik
Collaborator
Collaborator

 

Hi @tonythm you can find a lot of literature, in this forum too, but just for you info as guide here below a simple code.

The code is starting from an Excel empty sheet already opened. If you wand to open you should app:

On Error Resume Next
Set objExcel = GetObject(, "Excel.Application")  'already indicate below                                           
If Err.Number > 0 Then
    Set objExcel = CreateObject("Excel.Application") ' start a new excel session.
End If

 

 

Set ExcelFileOpened = GetObject(, "Excel.Application") ' create the Excel Object
Set wrks = ExcelFileOpened.ActiveSheet ' point to active sheet
'fix a reference for column and row for next use.
ExRow = 1
ExCol = 1
'WRITE COLUMN HEADING
wrks.Cells(ExRow , ExCol) = "COLUMN HEADING"
wrks.Cells(ExRow , ExCol + 1) = "COLUMN HEADING 2"
wrks.Cells(ExRow , ExCol + 2) = "COLUMN HEADING 3"
' SEARCH BLOCK ON DRAWING.
For Each ENTITY In ThisDrawing.ModelSpace                                                   '  For eache entity inside modele space
    If ENTITY.ObjectName = "AcDbBlockReference" Then                                        ' 
        If ENTITY.EffectiveName = "Block name 1" Or ENTITY.EffectiveName = "block name 2" Then  ' check block name inside the dwg
            If ENTITY.HasAttributes = True Then                                             ' Se il blocco ha attributi
                ATTRIB_LIST = ENTITY.GetAttributes                                          ' GET THE ARRAY "ATTRIB_LIST" 
                INS_POINT = ENTITY.InsertionPoint                                           ' BLOCK INSERTION POINT
	        wrks.Cells(ExRow + 2, ExCol) = ENTITY.name                                  ' BLOCK NAME
                    wrks.Cells(ExRow + 2, ExCol + 1) = ENTITY.Layer                         ' BLOCK LAYER
                    wrks.Cells(ExRow + 2, ExCol + 2) = ENTITY.EffectiveName                 ' BLOCK EFFECTIVE NAME
                    wrks.Cells(ExRow + 2, ExCol + 3) = INS_POINT(0)				    ' BLOCK INSERTION POINT X
                    wrks.Cells(ExRow + 2, ExCol + 4)  = INS_POINT(1)				    ' BLOCK INSERTION POINT Y
                    wrks.Cells(ExRow + 2, ExCol + 5) = INS_POINT(2)				    ' BLOCK INSERTION POINT Z
                    wrks.Cells(ExRow + 2, ExCol + 6) = ATTRIB_LIST(0).TextString            ' WRITE ON EXCEL ATTRIBUTE VALUE N.1
                    wrks.Cells(ExRow + 2, ExCol + 7) = ATTRIB_LIST(1).TextString       	    ' WRITE ON EXCEL ATTRIBUTE VALUE N.2 
                    wrks.Cells(ExRow + 2, ExCol + 8 = ATTRIB_LIST(2).TextString            ' WRITE ON EXCEL ATTRIBUTE VALUE N.3
                    wrks.Cells(ExRow + 2, ExCol + 9) = ATTRIB_LIST(3).TextString            ' WRITE ON EXCEL ATTRIBUTE VALUE N.4
                    ExRow = ExRow + 1
                    i = i + 1
                End If
            End If
        End If
        End If
Next

 

 

please note that above code it's a simple guide line, for example if the two different blocks you mentioned has different amount of attribute, the code shall be fixed as you need.

How ever that should be a good starting point.