User-defined type Error

User-defined type Error

Anonymous
Not applicable
5,339 Views
3 Replies
Message 1 of 4

User-defined type Error

Anonymous
Not applicable

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

0 Likes
Accepted solutions (2)
5,340 Views
3 Replies
Replies (3)
Message 2 of 4

Ed__Jobe
Mentor
Mentor
Accepted solution

It sounds like you haven't set a reference to the AutoCAD type library. In the xl vba ide, go to Tools>References and select the AutoCAD 20XX Type Library.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 3 of 4

Alfred.NESWADBA
Consultant
Consultant
Accepted solution

Hi,

 

>> 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"

You have to add references to the VB-project like here:

 

20170131_1805.png

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
Message 4 of 4

Anonymous
Not applicable

Thank you very much, both of you!

0 Likes