Message 1 of 4

Not applicable
01-31-2017
08:54 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi,
I am trying to write a program to do the following :
-Select a folder containing AutoCAD blocks;
-Get the volume of the block (containing several 3D solid elements);
-Copy the information into an Excel table.
I am writing the program from VBA in Excel, as i want to launch it from an Excel master file.
The problem I have is that I can't get AutoCAD object types to compile (such as AcadEntity and Acad3dSolid). The error message will be "User-defined type not defined". I tried to use the "set" command without much success.
If I copy/paste the code in the AutoCAD VBA, the AutoCAD-specific variable type will compile, but not the Excel-specific lines.
Here is what I have now :
Option Explicit Sub LoopAllFilesInFolder() 'SOURCE: www.TheSpreadsheetGuru.com 'MODIFIED BY: Etienne Dim myPath As String Dim myFile As String Dim myExtension As String Dim FldrPicker As FileDialog Dim AcadApp As Object Dim mydwg As Object Dim myBlock As String 'Optimize Macro Speed 'Application.ScreenUpdating = False 'Application.EnableEvents = False 'Application.Calculation = xlCalculationManual 'Retrieve Target Folder Path From User Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker) With FldrPicker .Title = "Select A Target Folder" .AllowMultiSelect = False If .Show <> -1 Then GoTo NextCode myPath = .SelectedItems(1) & "\" End With 'In Case of Cancel NextCode: myPath = myPath If myPath = "" Then GoTo ResetSettings 'Target File Extension myExtension = "*.dwg" 'Target Path with Ending Extention myFile = Dir(myPath & myExtension) 'Loop through each Autocad file file in folder Do While myFile <> "" 'Set variable equal to opened workbook myBlock = myPath & myFile Set AcadApp = GetObject(, "AutoCAD.Application") Set mydwg = AcadApp.Documents.Open(myBlock) 'Ensure Workbook has opened before moving on to next line of code DoEvents Call CalculateMass(mydwg) 'Doesn't save and Close Workbook mydwg.Close SaveChanges:=False 'Ensure Workbook has closed before moving on to next line of code DoEvents 'Get next file name myFile = Dir Loop 'Message Box when tasks are completed MsgBox "Task Complete!" ResetSettings: 'Reset Macro Optimization Settings Application.EnableEvents = True Application.Calculation = xlCalculationAutomatic Application.ScreenUpdating = True End Sub Sub CalculateMass(mydwg As Object) Dim hasBlock As Boolean Dim tEnt As AcadEntity Dim tEntSolid As Acad3DSolid Dim totalVolume As Double Dim volume As Double 'Set tEnt = AutoCAD.AcadApplication.ActiveDocument.ModelSpace.Entity hasBlock = False totalVolume = 0 For Each tEnt In ThisDrawing.ModelSpace If TypeOf tEnt Is Acad3DSolid Then Set tEntSolid = tEnt volume = tEntSolid.volume totalVolume = totalVolume + volume End If If TypeOf tEnt Is AcadBlock Then hasBlock = True End If Next End Sub
Solved! Go to Solution.