- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
AutoCAD 2024 Developer and ObjectARX Ajuda | Example: List AutoCAD attributes on an Excel spreadshee...
I'm trying to automate the process of reading DWG files and exporting attribute values to Excel using VBA, similar to the Autodesk example. However, the method doesn't seem to work in AutoCAD 2025, and after several attempts, I was unable to get it to read the attribute values. Did something change in the AutoCAD API that broke the previous approach?
My script is setup to read the block PRANCHA and then find the attributes on it and extract its values, but it doesn't..
Sub ExtrairAtributosMultiplosArquivosR002()
' Declarações
Dim AcadApp As Object
Dim AcadDoc As Object
Dim AcadBlockRef As Object
Dim AcadAttribute As Object
Dim ws As Worksheet
Dim Row As Long
Dim FileDialog As FileDialog
Dim SelectedFiles As FileDialogSelectedItems
Dim FileName As Variant ' Ajustado para Variant
Dim AutoCADInstances As Object
Dim ConfirmClose As VbMsgBoxResult
On Error GoTo ErrorHandler
' Limpar debug
Debug.Print String(50, "-") & " Início do Processamento " & String(50, "-")
' Fechar instâncias do AutoCAD, se houver
On Error Resume Next
Set AutoCADInstances = GetObject(, "AutoCAD.Application")
On Error GoTo ErrorHandler
If Not AutoCADInstances Is Nothing Then
ConfirmClose = MsgBox("Há instâncias do AutoCAD abertas. Deseja fechá-las e continuar?", vbYesNo + vbExclamation, "Fechar AutoCAD")
If ConfirmClose = vbNo Then
Debug.Print Now & " - Processo cancelado pelo usuário."
Exit Sub
Else
AutoCADInstances.Quit
Debug.Print Now & " - Todas as instâncias do AutoCAD foram fechadas."
End If
End If
' Inicializar AutoCAD
Set AcadApp = CreateObject("AutoCAD.Application")
AcadApp.Visible = False
Debug.Print Now & " - AutoCAD inicializado com sucesso."
' Configurar planilha de saída
Set ws = ThisWorkbook.Sheets(1)
ws.Cells.Clear
Row = 1
' Seleção de múltiplos arquivos
Set FileDialog = Application.FileDialog(msoFileDialogFilePicker)
With FileDialog
.Title = "Selecione arquivos DWG"
.Filters.Clear
.Filters.Add "Desenhos do AutoCAD", "*.dwg"
.AllowMultiSelect = True
If .Show = -1 Then
Set SelectedFiles = .SelectedItems
Else
Debug.Print Now & " - Nenhum arquivo selecionado. Processo cancelado."
Exit Sub
End If
End With
' Processar cada arquivo selecionado
For Each FileName In SelectedFiles
Debug.Print Now & " - Abrindo o arquivo DWG: " & FileName
Set AcadDoc = AcadApp.Documents.Open(FileName)
Debug.Print Now & " - Arquivo aberto com sucesso: " & FileName
' Escrever o nome do arquivo no Excel
ws.Cells(Row, 1).Value = "Arquivo"
ws.Cells(Row, 2).Value = FileName
Row = Row + 1
' Verificar PaperSpace
Debug.Print Now & " - Iniciando leitura do PaperSpace..."
For Each AcadBlockRef In AcadDoc.PaperSpace
On Error Resume Next
' Verificar se é um bloco com atributos
If AcadBlockRef.ObjectName = "AcDbBlockReference" Then
If AcadBlockRef.Name = "PRANCHA" Then
Debug.Print Now & " - Bloco PRANCHA encontrado."
' Iterar pelos atributos do bloco
For Each AcadAttribute In AcadBlockRef.GetAttributes
ws.Cells(Row, 1).Value = AcadAttribute.TagString ' Tag do atributo
ws.Cells(Row, 2).Value = AcadAttribute.TextString ' Valor do atributo
Debug.Print Now & " - Atributo encontrado: " & AcadAttribute.TagString & " = " & AcadAttribute.TextString
Row = Row + 1
Next AcadAttribute
End If
End If
On Error GoTo ErrorHandler
Next AcadBlockRef
Debug.Print Now & " - Arquivo fechado: " & FileName
AcadDoc.Close False
Next FileName
' Fechar AutoCAD
AcadApp.Quit
Debug.Print Now & " - Instância do AutoCAD fechada."
Debug.Print Now & " - Processamento concluído."
MsgBox "Processamento concluído com sucesso!", vbInformation
Exit Sub
ErrorHandler:
Debug.Print Now & " - Erro: " & Err.Description
If Not AcadApp Is Nothing Then AcadApp.Quit
MsgBox "Ocorreu um erro: " & Err.Description, vbCritical
End Sub
Solved! Go to Solution.