Message 1 of 6

Not applicable
09-25-2019
06:03 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi everybody. Im new to VBA in autocad and im trying to extract the attributes of specific block to Excel. The block is a drawing header and the name of it is "A1 - NME".
I found the code below on the internet and it work very well, except it extracts all blocks with attributes in the drawing. Is it possible to change the code so it only extracts blocks with the name "A1 - NME"?
Option Explicit Sub Extract() If ThisDrawing Is Nothing Then MsgBox "Please open a drawing file and then restart this macro." Exit Sub End If '------------------------------------------------------- 'Excel setup Dim Excel As Application Dim MySheet As Excel.Worksheet Dim BlckNameRng As Excel.Range, TagsRng As Excel.Range, myCell As Excel.Range Dim iniColStr As String Dim iniRow As Long, iniCol As Long ' handling excel application On Error Resume Next Set Excel = GetObject(, "Excel.Application") If Err Then Set Excel = CreateObject("Excel.Application") ' handling workbook and worksheet With Excel .Visible = True Set MySheet = .ActiveWorkbook.ActiveSheet If Err Then .Workbooks.Add Set MySheet = .ActiveWorkbook.ActiveSheet End If On Error GoTo 0 End With 'handling columns where to start writing data from iniColStr = "J" '<-- Here you specify which column to start writing data from iniRow = 1 '<-- Here you specify which row to start writing data from iniCol = MySheet.Range(iniColStr & "1").Column Set BlckNameRng = MySheet.Cells(iniRow, iniCol).Resize(, 1000) ' this will clear excel cells in 1000 columns right of the initial one Set TagsRng = BlckNameRng.Offset(1) With BlckNameRng .EntireColumn.Clear With .Font .Bold = True .color = 1152 End With End With '------------------------------------------------------- '------------------------------------------------------- 'blocks reference searching&handling Dim myBlckRef As AcadBlockReference Dim Attrs As Variant Dim nBlckRefs As Integer, iBlckRef As Integer, nAttrs As Integer, iAttr As Integer, nTags As Integer Dim BlckName As String, BlckHandle As String Dim gpCode(0) As Integer Dim dataValue(0) As Variant Dim ssetObj As AcadSelectionSet Dim myRow As Long, myCol As Long Dim LBnd As Integer, Ubnd As Integer 'selecting block references in the active drawing gpCode(0) = 0 dataValue(0) = "INSERT" On Error Resume Next Set ssetObj = ThisDrawing.SelectionSets.Add("BlockRefSset") If Err <> 0 Then Set ssetObj = ThisDrawing.SelectionSets.Item("BlockRefSset") Else ssetObj.Clear End If On Error GoTo 0 ssetObj.Select acSelectionSetAll, , , gpCode, dataValue 'handling block references found nTags = 0 ' this counter will keep track of the number of columns filled with blockreferences data ("handles" and "attributes") nBlckRefs = ssetObj.Count For iBlckRef = 0 To nBlckRefs - 1 Set myBlckRef = ssetObj.Item(iBlckRef) If myBlckRef.HasAttributes Then ' getting blockreference info With myBlckRef BlckName = .Name BlckHandle = .Handle Attrs = .GetAttributes End With LBnd = LBound(Attrs) Ubnd = UBound(Attrs) nAttrs = Ubnd - LBnd + 1 ' handling excel list structure consequent to blockreference blockname Set myCell = BlckNameRng.Find(BlckName, lookin:=xlValues) ' searching for blockname existence If myCell Is Nothing Then 'if the blockname hasn't already been met-> registered 'then we have to arrange new columns to house blockreference data (handle and attributes tagstrings and textstrings) myCol = nTags + 1 ' setting ref column (where to start writing from) one to the right of the last one nTags = nTags + 1 + nAttrs ' update number of columns to be filled with data: the "1" is for the "handle" column ' writing block header cells With BlckNameRng(1, myCol) .Value = BlckName With .Resize(, nAttrs + 1) .Merge .BorderAround (xlContinuous) .HorizontalAlignment = xlCenter End With End With ' writing blockreference data header cells (handle and attributes tags) With TagsRng(1, myCol) .Value = "HANDLE" .BorderAround (xlContinuous) 'every block data heade is boxed For iAttr = LBnd To Ubnd With .Offset(0, 1 + iAttr - LBnd) .Value = Attrs(iAttr).TagString .BorderAround (xlContinuous) .HorizontalAlignment = xlCenter End With Next iAttr End With Else ' if the blockname has already been listed myCol = myCell.Column - BlckNameRng.Column + 1 'set ref column to the found cell one End If 'writing blockreference data cells With BlckNameRng.Offset(, myCol - 1).EntireColumn myRow = .Cells(.Rows.Count, 1).End(xlUp).Offset(1).Row ' getting the first free cell in the column With .Cells(myRow, 1) .Borders(xlEdgeLeft).LineStyle = xlContinuous ' left border the 1st column .Value = BlckHandle ' writing handle data For iAttr = LBnd To Ubnd .Offset(0, 1 + iAttr - LBnd).Value = Attrs(iAttr).TextString ' writing attributes string data Next iAttr .Offset(0, 1 + Ubnd - LBnd).Borders(xlEdgeRight).LineStyle = xlContinuous ' right border the last column End With End With End If Next iBlckRef '------------------------------------------------------- With BlckNameRng.CurrentRegion .Columns.AutoFit .Select End With Set Excel = Nothing End Sub
Solved! Go to Solution.