Hi @Anonymous ,
here below a simple code for extracting attributes from a block, and transfer to excel.
If Excel it's not opened the procedure open a new Excel session with an empty worksheet.
For doing the opposite I'll show you how to do, it's more or less the same way but reverse, you should be able to get value from excel cell and write in the correct position inside Attribute array coming from the previously extracted and transferred to Excel.
First of all you have to change inside the procedure the block name (now called "A1", "A2" and "A3"), later if selected block has attributes these will be stored in LISTA_ATTRIBUTI array variable.
There are also some check on Attribute Value ( value "-"), specific for my job, should be bypassed.
Inside LISTA_ATTRIBUTI you will have inside the Textstring property the attribute value, and inside TAGSTRING you will have the attribute "NAME", for changing it you have to know the position and write a code like LISTA_ATTRIBUTI (1).textstring=".....Excel cell Value...." ' (example to have cell value wrks.Cells (ROW, COLUMN).value).
Sub ESPORTA_ATTRIBUTI_BLOCCHI_P_AND_I()
Dim wrkb As Excel.Workbooks ' Attribuzione della variabile "Cartella di lavoro Excel"
Dim wrks As Excel.Worksheet ' Attribuzione della variabile "Foglio di lavoro"
RIGA = 2
COLONNA = 2
On Error Resume Next
Set objExcel = GetObject(, "Excel.Application") ' Attribusce alla variabile "objExcel" l'applicazione Excel
If Err.Number > 0 Then
Set objExcel = CreateObject("Excel.Application")
End If
objExcel.Visible = True ' Rende visivile Excel
Set wrkb = objExcel.Workbooks.Add ' Attribuisce alla variabile wrkb la cartella di lavoro Excel corrente
Set wrks = objExcel.ActiveSheet ' Attribuisce alla variabile wrks il foglio di lavoro
objExcel.Application.ScreenUpdating = False
wrks.Cells(RIGA, COLONNA) = "PROGRESSIVO BLOCCO"
wrks.Cells(RIGA, COLONNA + 1) = "NOME LAYER"
wrks.Cells(RIGA, COLONNA + 2) = "NOME EFFETTIVO"
'wrks.Range("D1") = "X"
'wrks.Range("E1") = "Y"
'wrks.Range("F1") = "Z"
wrks.Cells(RIGA, COLONNA + 3) = "TAG"
wrks.Cells(RIGA, COLONNA + 4) = "LOOP"
wrks.Cells(RIGA, COLONNA + 5) = "DESCRIPTION"
wrks.Cells(RIGA, COLONNA + 6) = "I/O"
wrks.Name = "Importazione" ' Nuova denominazione "Foglio1"
objExcel.Sheets("Foglio2").Delete ' Eliminazione del "Foglio2" in eccesso
objExcel.Sheets("Foglio3").Delete ' Eliminazione del "Foglio3" in eccesso
Dim VETTORE() As Variant
i = 1
ReDim Preserve VETTORE(NUMERO, 4)
For Each ENTITY In ThisDrawing.ModelSpace ' Per ogni entità nello spazio modello
If ENTITY.ObjectName = "AcDbBlockReference" Then ' Se l'entità è un blocco AUTOCAD
If ENTITY.EffectiveName = "A1" Or ENTITY.EffectiveName = "A2" Or ENTITY.EffectiveName = "A3" Or ENTITY.EffectiveName = "D1" Then ' Se il blocco si chiama come il valore impostato nelle variabile "BN"
If ENTITY.HasAttributes = True Then ' Se il blocco ha attributi
LISTA_ATTRIBUTI = ENTITY.GetAttributes ' Inserisce nell'array "LISTA_ATTRIBUTI" tutti gli attributi del blocco
PUNTO_INSERIMENTO = ENTITY.InsertionPoint ' Coordinate del punto di inserimento del blocco selezionato --> PUNTO_INSERIMENTO è una matrice "X", "Y", "Z"
If LISTA_ATTRIBUTI(0).TextString = "-" And LISTA_ATTRIBUTI(1).TextString = "-" And LISTA_ATTRIBUTI(2).TextString = "-" And LISTA_ATTRIBUTI(3).TextString = "-" Then
Else
wrks.Cells(RIGA + 2, COLONNA) = ENTITY.Name ' Nome blocco
wrks.Cells(RIGA + 2, COLONNA + 1) = ENTITY.Layer ' Layer blocco
wrks.Cells(RIGA + 2, COLONNA + 2) = ENTITY.EffectiveName ' Nome effettivo blocco
'wrks.Range("D" & RIGA) = PUNTO_INSERIMENTO(0)
'wrks.Range("E" & RIGA) = PUNTO_INSERIMENTO(1)
'wrks.Range("F" & RIGA) = PUNTO_INSERIMENTO(2)
wrks.Cells(RIGA + 2, COLONNA + 3) = LISTA_ATTRIBUTI(0).TextString ' Scrittura attributo N.1 - TAG
wrks.Cells(RIGA + 2, COLONNA + 4) = LISTA_ATTRIBUTI(1).TextString ' Scrittura attributo N.2 - LOOP
wrks.Cells(RIGA + 2, COLONNA + 5) = LISTA_ATTRIBUTI(2).TextString ' Scrittura attributo N.3 - DESCRIPTION
wrks.Cells(RIGA + 2, COLONNA + 6) = LISTA_ATTRIBUTI(3).TextString ' Scrittura attributo N.4 - I/O
RIGA = RIGA + 1
i = i + 1
End If
End If
End If
End If
Next
End sub
I'm sorry but inside the code there are comments in ITALIAN language.
You asked also for apply the procedure on a lot of drawing, I'll show you later how I managed this issue in past.
Let me know.
Bye