Failure of VBA to connect to autocad (excel)

Failure of VBA to connect to autocad (excel)

Anonymous
Not applicable
6,441 Views
22 Replies
Message 1 of 23

Failure of VBA to connect to autocad (excel)

Anonymous
Not applicable

Have an application that worked for years, not no longer connects to autocad.  Using 2019 Electrical, with Excel 2016.

 

This is a section of the code that fails consistently with fault 429 - ActiveX component cant create object.

 

Any pointers would be greatly appreciated

 

 

Sub ConnectToAcad()
Dim AcadApp As AcadApplication
Set AcadApp = CreateObject("AutoCAD.Application")
' have tried - Set AcadApp = GetObject(,"AutoCAD.Application") ----- with same results
' have tried - Set AcadApp = New AcadApplication ----- with same results
AcadApp.Visible = True

AcadApp.ActiveDocument.Utility.Prompt "Excel Connected"

 

End Sub

0 Likes
6,442 Views
22 Replies
Replies (22)
Message 2 of 23

Ed__Jobe
Mentor
Mentor

The two functions work together. If you can't get an object in the Running Object Table (ROT), then you create it. Try the function below.

 

'main sub
Dim AcadApp As AcadApplication
Set AcadApp = GetAcad()

Public Function GetAcad() As AcadApplication
    On Error Resume Next
    Dim acApp As AcadApplication
    Set acApp = GetObject(, "AutoCAD.Application")
    If acApp Is Nothing Then
        Set acApp = CreateObject("AutoCAD.Application")
    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 3 of 23

Anonymous
Not applicable

Thanks for quick reply, after adding your function, I get the following:

your function consistantly sets acAPP to 'Nothing'   does not matter if ACAD application is running or not.

If On Error is eliminated the Set acApp line returns run time error 13 - type mismatch. 

 

With error resume active, the 'If acApp is Nothing' line fires, and the set acApp still returns NOTHING.

 

Any other Ideas?

I have verified proper mapping for references.  Nothing (other than standard windows updates) have changed on base machine.

 

0 Likes
Message 4 of 23

Ed__Jobe
Mentor
Mentor

It works for me with 2018. Open Regedit.exe and navigate to Computer\HKEY_CLASSES_ROOT\AutoCAD.Application. Following that should be some keys with numbers following. 2019 should be "AutoCAD.Application.22" if I remember right. Change your excel get/set functions to match what you have installed.

 

Try compiling from the Debug menu to make sure there are no compile errors.

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 23

Anonymous
Not applicable
Appears as AutoCAD.Application.23 in CurVer , and that number matches all the other references in the registry.

Autocad itself launches and runs fine, just VBA links thru Excel have failed.



Not sure what you are referring to when you say 'excel get/set functions' The only reference I see are in the VBA project references and that is acax23enu.tlb and that file exists where indicated.



Yes, I have run debug application both WITH and WITHOUT ACAD running.

Getting same results regardless, never connects to running, or starts new instance of AutoCad.



As I indicated, this is a long time running VBA application. I believe I have narrowed the problem down to the indicated lines.

.....that's what seems so odd, its like Excel VBA cant see AutoCad when running, or start new one when not running.

Thanks again for your assistance, running out of ideas.




0 Likes
Message 6 of 23

Ed__Jobe
Mentor
Mentor

I'm referring to the GetObject and SetObject functions. Change them to match the classID in the registry.

GetObject(, "AutoCAD.Application.23")

SetObject("AutoCAD.Application.23")

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 23

Anonymous
Not applicable
Again, I cant thank you enough for looking at this.

Below is what I have along with errors shown when each option is tried.



Public Function GetAcad() As AcadApplication

On Error Resume Next

Dim acApp As AcadApplication

Set acApp = GetObject(, "AutoCAD.Application.23") '<---- without debug generates Runtime Error 13 - Type mismatch

If acApp Is Nothing Then

Set acApp = CreateObject("AutoCAD.Application.23") '<---- always returns 'Nothing' when triggered

End If

Set GetAcad = acApp

End Function



Sub ConnectToAcad()

Set AcadApp = GetAcad()

' Dim AcadApp As AcadApplication '<<-- replaced with fcn call above - same errors regardless

' Set AcadApp = CreateObject("AutoCAD.Application.23") ' <---- Run Time Error 429 - Unable to Create ActiveX Object

' Set AcadApp = GetObject(, "AutoCAD.Application.23") ' <----- Run Time Error 13 - Type Mismatch

' Set AcadApp = New AcadApplication ' <----- Run Time Error - (800702e4) Request Requires Evaluation

AcadApp.Visible = True '<--- Run Time Error 91 - Object Variable or With Block Variable not set





AcadApp.ActiveDocument.Utility.Prompt "Excel Connected"

End Sub
















0 Likes
Message 8 of 23

Ed__Jobe
Mentor
Mentor

Did you run Debug>Compile?

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

Anonymous
Not applicable
Debug Compile option is shadowed out.

Normally I just open VBA thru Excel, set break points, and go....






0 Likes
Message 10 of 23

Ed__Jobe
Mentor
Mentor

If the compile option is greyed out, then the code is already compiled. The code I gave you works for me with XL 2016 and AutoCAD 2018. The structure of the code hasn't changed in years.

 

Try a clean test of xl. Start a new blank worksheet and paste the function into ThisWorkbook module. Set the reference for AutoCAD 2019 set a breakpoint. Step through the code. If this works, then there is something in you old module that is causing the problem. If it doesn't work, there can only be two causes left.

 

1. One of the references in the project is invalid. For example, you are using the wrong version of AutoCAD TLB. Or, I've seen where a problem with one reference can cause errors in other tlb's, like VBA directives.

2. There is something corrupt in your AutoCAD installation. The reference to the AcadApplication type is not valid for the object being returned by the GetObject method, hence a type mismatch. I would try a clean install of AutoCAD using the following steps.

 

1. Go to the start menu and under your AutoCAD folder, run the app Export AutoCAD 2019 Settings. Save the settings to any folder of your choice.

2. Go to the start menu and under your Autodesk folder, run Uninstall Tool.

3. Reinstall AutoCAD

4. Go to the start menu and under your AutoCAD folder, run the app Import AutoCAD 2019 Settings using the previously saved settings.

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 11 of 23

Anonymous
Not applicable
Thank you for your assistance, will try reloading autocad.




0 Likes
Message 12 of 23

Ed__Jobe
Mentor
Mentor

Did you first try the new workbook test? How did that go?

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 13 of 23

Anonymous
Not applicable
Thanks for checking back, unfortunately I need to talk to our IT department to get a clean install of ACAD.

Hopefully they will be able to have it done by tomorrow. Will continue debugging at that time.

Thanks again for your pointers.




0 Likes
Message 14 of 23

Ed__Jobe
Mentor
Mentor

Before you reinstall AutoCAD, be sure that's the problem. Try this test. I changed the variable type from AcadApplication to Variant. There is no reference to an acad tlb. Run the function in the attached xlsm. A breakpoint is already set. Check the Locals window to make sure that the variant is getting set with the right type of object.

2021-04-23_7-52-13.png

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 15 of 23

Anonymous
Not applicable

Sorry for long delay on response, got called out of office.

As reminder - 2019 electrical on Win 10x64 with Office 16 excel.

Application was functional hundreds of times, not consistently fails to connects to Autocad, does not matter if cad is already running or not, all evaluations show Error -

 

Have reinstalled Autocad, Excel, VB....

Have verified that both Excel and Autocad both are running with Admin privileges.

 

Attempting proposed solution generates same result, ActiveX component cant create object.

As a side note, using Dim acadApp as AcadApplication, and then attempting to set the acadApp variable generates requested operation required elevation for all of the following:

 

Set acadApp = CreateObject("AutoCad.Application")  ' with or without .23

Set acadApp = New AcadApplication

 

 

The variables remain 'Empty' never turning into array.

Suggested Variant variable remains 'Variant/Empty'

 

Have attached stripped down XLSM file showing resultant errors for each line

0 Likes
Message 16 of 23

Ed__Jobe
Mentor
Mentor

Did you try the running the app I sent you in post 14? To see if there's something wrong with you project or it's something with the system, run the simplest test you can.

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 17 of 23

Anonymous
Not applicable
Yes, app produced the same error - ActiveX component cant create object.

Seems to be like it cannot do set acadApp = CreateObject, or = NewApplication, so all further commands to 'GetObject' fail with ActiveX component cant create object.



Did not matter if definition was as AcadApplication, Object, or Variant.

Watch list never shows variable acadApp as anything except 'empty'



Drilling down to application running as ADMIN or not, have tried both ways but appears that even with both excel and autocad set to ADMIN keep getting error 'requested operation required elevation'

Assuming 'elevation' indicates excel unable to launch autocad....




0 Likes
Message 18 of 23

Ed__Jobe
Mentor
Mentor

I tried the same scenario with the same apps and it works. The test you ran shows that its something with your system. Since you reinstalled the apps, I don't know what else it could be, just that it's not a problem with the code or vba project. The only other thing I can suggest is to manually do a "clean uninstall" to remove every trace of AutoCAD before trying to reinstall. Do you have multiple versions of AutoCAD installed?

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 19 of 23

grobnik
Collaborator
Collaborator

Hi @Anonymous question could be stupid, but just for test are you able to transfer the procedure on Autocad side ? for example opening automatically an excel file ? it's just to try perhaps the issue could be on excel side and not on AutoCAD side.

0 Likes
Message 20 of 23

Anonymous
Not applicable
Think I found it, Appears that both the ACAD and EXCEL applications inherited the permissions from my D drive and would not allow creation, its only an archive drive.

I have removed all inherited permissions and made the implicit. Now it appears that VB can connect to autocad properly. Now back to the original application to see if it works for the remainder of drawing creation tasks.



Thanks for your help.


0 Likes