Cannot detect autocad 2023 instance

Cannot detect autocad 2023 instance

gus_lindquist
Explorer Explorer
916 Views
8 Replies
Message 1 of 9

Cannot detect autocad 2023 instance

gus_lindquist
Explorer
Explorer

Hello.  I use VBA scripting through Excel to interact with Autocad.  I currently run 2017 and have run 2021 as well in the past.  I have installed the VBA module from the autocad website (https://knowledge.autodesk.com/support/autocad/troubleshooting/caas/downloads/content/download-the-m...)

for both of these, as is evidenced by the presence of "Autocad 2017 Type Library" and "Autocad 2021 Type Library" in my Available References from my Tools->References of the developer tab from Excel.

 

However, after installing the 2023 VBA module, I do not see a 2023 Type Library.  Furthermore, I am unable to connect to my autocad 2023 instance from Excel.

 

I have uninstalled and reinstalled the module and autocad a couple of times, and deleted everything appeared relevant in my Temp folder.

 

Does anyone else have any other ideas?

 

Thanks!

 

0 Likes
917 Views
8 Replies
Replies (8)
Message 2 of 9

Ed__Jobe
Mentor
Mentor

Have you successfully installed 2023 vba? Open AutoCAD 2023 and type VBAMAN. If the dialog opens, then you have successfully installed the vba plugin. If not, you will get a notice that it is not installed. You have to get this working before Excel can reference it.

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 9

gus_lindquist
Explorer
Explorer

I do get the dialog.

 

Basic question:  Should I have a 2023 Type Library in my Excel VBA references similar to what I had for 2017 and 2021?  Or has this changed with 2023?

0 Likes
Message 4 of 9

Ed__Jobe
Mentor
Mentor

I am running acade 2023. I checked my references and it's using the 2021 tlb. So I guess no changes were made to the tlb since 2021. However, when you connect with GetObject/CreateObject, you will still have to use the 2023 classid.

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 5 of 9

gus_lindquist
Explorer
Explorer

Thanks for the info thusfar! 

 

Can you be more specific on the 2023 class id?  Currently, I do, and have been doing throughout 2017 and 2021:

 

Public AcadApp As AcadApplication

Set AcadApp = GetObject(, "Autocad.Application")

 

I'm assuming you are suggesting a different argument to GetObject?

 

Also, FWIW, this is in a VBA script with an "On Error Resume Next" where it is throwing an err.  The details of the err are attached, and interesting.  "Type mismatch" is the desc.

 

 

 

 

0 Likes
Message 6 of 9

Ed__Jobe
Mentor
Mentor

The class id is how the OS identifies software. If you only supply "Autodesk.AutoCAD", it may be finding other versions and hence, the type mismatch. The class id for 2023 is "Autodesk.AutoCAD.24.2". You can always double check what you have installed by opening REGEDIT and going to HKEY_CLASSES_ROOT\AutoCAD.Application.

 

acad class id.png

 

I always recommend putting that kind of functionality in a separate function.

Public Function GetAcad(Optional ver As String) As AcadApplication
    ' support multiple acad versions.
    'Sample ver for AutoCAD 2023 ' ".24.2"
    On Error Resume Next
    Dim acApp As AcadApplication
    Dim clsid As String
    clsid = "AutoCAD.Application"
    If Not ver = "" Then
        clsid = clsid & ver
    End If
    Set acApp = GetObject(, clsid)
    If acApp Is Nothing Then
        Set acApp = CreateObject(clsid)
    End If
    Set GetAcad = acApp
End Function

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 7 of 9

gus_lindquist
Explorer
Explorer

Thanks for your help.  Sadly, I'm still having significant issues though it is kind of a moving target.  I had the AcadApplication being well defined (to a global scope object) but in my drawing module it was Nothing again (resulting in my script bombing on my InsertBlock method).  Perhaps a scope issue.  Suddenly, I couldn't even get 2017 to work anymore.  Had to uninstall 2023 VBA module and reboot to get it to work again, but then I reinstalled it and can no longer get a well defined AcadAppilication despite the ".24.2" appended as you suggest, and as got me further on my last attempt.  

 

I'm still bashing this thing so maybe something will eventually work.  Just wanted to thank you for the help so far.

0 Likes
Message 8 of 9

gus_lindquist
Explorer
Explorer

Oops, hadn't reselected the 2021 Type Library.

 

So, my AcadApplication is persisent and well defined in global scope, but I still can't InsertBlock in 2023.  Again, don't feel like I've exhausted necessarily everything, but this is a bit of a surprise since the initial connection does appear to work.

 

0 Likes
Message 9 of 9

Ed__Jobe
Mentor
Mentor

If you want more help, you'll need to show some code. I'm not sure what you mean by 'global scope'. If you want to connect to 2017, then using my function, it would look like.

Set acapp = GetAcad(".21.0")

 

You should be able to have multiple versions of acad installed with multiple versions of the vba enabler. They are version independent. However, in your xl app, you can only reference one acad tlb at a time. If you want xl to support multiple versions of acad, then you need to determine what version is installed and set a reference at runtime. So, during development, set a reference and get all your bugs ironed out. Then when deploying, remove the reference and set it at runtime using the function below.You have to reference "Visual Basic for Applications Extensibility 5.3".


Public Function SetReferenceByFile(FilePath As String, Optional DisplayErrors As Boolean) As VBIDE.Reference
    'returns True if successful in setting a reference in the current dvb,
    'given the FilePath of a tlb, olb, dll. The DisplayErrors option determines
    'whether or not an error message will be displayed upon erroring.
    On Error GoTo ErrorHandler
    Dim objVB As Object  'VBE
    Dim objRef As VBIDE.Reference
    Dim objRefs As VBIDE.References
    Dim strPath As String
    
    Set objVB = AcadApplication.VBE
    Set objRef = objVB.ActiveVBProject.References.AddFromFile(FilePath)
    SetReferenceByFile = True
    GoTo Finish:
ErrorHandler:
    If Err.Number = 32813 Then
        'reference was already set, just return the reference
         Set objRefs = objVB.ActiveVBProject.References
         For Each objRef In objRefs
             If objRef.FullPath = FilePath Then
                 Set SetReferenceByFile = objRef
             End If
         Next objRef
    Else
        If DisplayErrors = True Then
            MsgBox Err.Number & ", " & Err.Description, vbExclamation, "SetReferenceByFile."
        Else
        
        End If
        Set SetReferenceByFile = Nothing
    End If
Finish:
    Set objVB = Nothing
    Set objRef = Nothing
End Function

 

The other way is to use late binding, i.e. declare acapp as Object and let vba determine the data type at runtime. Again, for development, to take advantage of Intellisense, set a reference to one tlb and write your code. Then just before deployment, change all the acad data types to Object.

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