Automation error - OwnerID in Excel

Automation error - OwnerID in Excel

miguelmachadoecosta
Advocate Advocate
2,374 Views
6 Replies
Message 1 of 7

Automation error - OwnerID in Excel

miguelmachadoecosta
Advocate
Advocate

I'm getting this error when using the OwnerID property and I think it may be related with the version of Excel.

 

It occurs for example when I have a line with: MsgBox Obj.OwnerID

 

This line works correctly in a 64bits computer with Excel 2013 64 bits but makes Excel crash with an error in all 64 bits computers I tried with Excel 32 bits. All other AutoCAD functions seem to work correctly so far.

 

I got this message with Windows 10; 64 bits; Excel 2013 32 bits; AutoCAD 2018:

Capture.JPG

 

 

 

 

 

 

 

 

And this message with Windows 7; 64 bits; Excel 2007 32 bits; AutoCAD 2016:Capture2.JPG

 

 

 

 

 

 

 

 

It worked fine with Windows 10; 64 bits; Excel 2013 64 bits.

 

Should this error be happening only with OwnerID? Is it because of Excel's version or there might be another reason? Excel always crashes after the error.

 

Thanks in advance.

0 Likes
Accepted solutions (2)
2,375 Views
6 Replies
Replies (6)
Message 2 of 7

norman.yuan
Mentor
Mentor
Accepted solution

Since the AutoCAD is 64-bit, in its COM object model, the ObjectId/OwnerId is 64-bit integer (type of LongLong), which is not supported in 32-bit environment (your 32-bit Excel), thus the run-time error when the line of code containing something like xxxx.ObjectID or xxxx.OwnerID is executed.

 

In fact, in Excel VBA editor, after setting reference to AutoCAD type library, if you click menu "Debug->Compile VBAProject", Excel would be frozen/crash (this is what happens to my computer with 64-bit Acad2018 and 32-bit Excel).

 

However, there is a workaround you could try: use late binding. That is, if you need to access property ObjectID/OwnerID, you declare the property owner as "object". This code works for me:

 

Option Explicit

Public Sub CadATest()

    Dim cadApp As object
    On Error Resume Next
    
    Set cadApp = GetObject(, "AutoCAD.Application")
    
    If Err.Number = 0 Then
        SearchEntities cadApp
    Else
        MsgBox "Cannot connect to a running AutoCAD session!"
    End If
    
End Sub

Private Sub SearchEntities(cadApp As AcadApplication)

    Dim dwg As object
    Dim ent As object
    
    Set dwg = cadApp.ActiveDocument
    
    If Not dwg Is Nothing Then
        For Each ent In dwg.ModelSpace
            MsgBox ent.OwnerID
        Next
    End If
    
End Sub

With this case, there is no reference to AutoCAD type library set. 

 

If you want to take advantage of early binding's intelisense, you can still set reference to AutoCAD type library, and declare most object to specific type, but make sure to declare those object as "object", to which you want to access  its ObjectID/OwnerID. This code also works for me 

 

Option Explicit

Public Sub CadATest()

    Dim cadApp As AcadApplication
    On Error Resume Next
    
    Set cadApp = GetObject(, "AutoCAD.Application")
    
    If Err.Number = 0 Then
        SearchEntities cadApp
    Else
        MsgBox "Cannot connect to a running AutoCAD session!"
    End If
    
End Sub

Private Sub SearchEntities(cadApp As AcadApplication)

    Dim dwg As AcadDocument
    Dim ent As Object
    
    Set dwg = cadApp.ActiveDocument
    
    If Not dwg Is Nothing Then
        For Each ent In dwg.ModelSpace
            MsgBox ent.OwnerID
        Next
    End If
    
End Sub

HTH

 

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 7

miguelmachadoecosta
Advocate
Advocate

Declaring as object works indeed. Thank you.

0 Likes
Message 4 of 7

miguelmachadoecosta
Advocate
Advocate

However ObjectIdToObject after getting the OwnerID doesn't seem to work. It fails with a type mismatch error.

0 Likes
Message 5 of 7

norman.yuan
Mentor
Mentor
Accepted solution

Well, it looks like the only way is to entirely use late binding. This code works for me:

Option Explicit

Public Sub Test()

    On Error Resume Next
    
    Dim cadApp As Object
    Set cadApp = GetObject(, "AutoCAD.Application")
    If cadApp Is Nothing Then Exit Sub
    
    Dim dwg As Object
    Set dwg = cadApp.ActiveDocument
    If dwg Is Nothing Then Exit Sub
    
    Dim ent As Object
    Dim id As Variant
    Dim obj As Object
    
    For Each ent In dwg.ModelSpace
        If UCase(ent.ObjectName) = "ACDBCIRCLE" Then
            id = ent.OwnerID
            MsgBox "Owner ID=" & CLng(id)
            Exit For
        End If
    Next
    
    If obj Is Nothing Then
    
        Set obj = dwg.ObjectIdToObject(id)
        MsgBox obj.ObjectName
        
    End If
    
End Sub

HTH

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 6 of 7

miguelmachadoecosta
Advocate
Advocate

Thank you. It worked again. It suffised to declare ThisDrawing and plineObj as object. For blockObj it was not necessary.

 

Dim blockObj As AcadBlock
Set blockObj = ThisDrawing.ObjectIdToObject(plineObj.OwnerID)
0 Likes
Message 7 of 7

jeremye86
Advocate
Advocate

Thanks Norman, your advice helped me.  I was was having type mismatch error when getting the objectID of an attribute defenition of a multileader object due to my excel 32 bit version.  It worked when I changed

dim  oML as AcadMLeader   to    dim  oML as Variant  or   dim  oML as object

 

this was the line that gave me the error

oML.SetBlockAttributeValue oAttDef_tagnumber.ObjectID, .Cells(irow, "d")