Hi everybody,
I found this code in the web to open AutoCAD using Excel VBA, but it does not work. I have looked for some alternatives but no one seems to work. I get the error message " 429 ActiveX component can't create object". I have already turned on the AutoCAD Type Library (which says 2019 instead of 2020). I am using Windows 10, MS Office 365 Excel and AutoCAD 2020. Any suggestions?
Could you try to use below code:
Sub Opendwg() Dim acadApp As Object Dim acadDoc As Object 'Check if AutoCAD application is open. If is not opened create a new instance and make it visible. Set acadApp = GetObject(, "AutoCAD.Application") Set acadDoc = acadApp.ActiveDocument If acadApp Is Nothing Then Set acadApp = CreateObject("AutoCAD.Application") acadApp.Visible = True End If End sub
Of course above code shall be written inside excel development area
As first test try to start with Autocad already opened and then try to start the above code with Autocad Closed.
I guess it should work
I tested exactly your code in Excel 2016 of my computer with Acad2020. It works as expected. Yes, the AutoCAD Type Library for AutoCAD2020 is the same as AutoCAD 2019 (I have exactly the same referenced in the "References" dialog box as yours.
So, I suspect there is something you did not describe, such as: do you run the Excel VBA code by you as your computer's logged in user, who also runs AutoCAD, or you somehow run the Excel VBA in a different manner (an unattended automation process), maybe?
Norman Yuan
@grobnik wrote:
Could you try to use below code:
Sub Opendwg() Dim acadApp As Object Dim acadDoc As Object 'Check if AutoCAD application is open. If is not opened create a new instance and make it visible. Set acadApp = GetObject(, "AutoCAD.Application") Set acadDoc = acadApp.ActiveDocument If acadApp Is Nothing Then Set acadApp = CreateObject("AutoCAD.Application") acadApp.Visible = True End If End subOf course above code shall be written inside excel development area
As first test try to start with Autocad already opened and then try to start the above code with Autocad Closed.
I guess it should work
Well, your code would break immediately at the line (red-highlighted), if there is no existing AutoCAD session running, because you do not have error handling (as OP did by "On Error Resume Next"). Without proper error handling, the "If acadApp Is Nothing Then..." portion will NEVER be executed.
Norman Yuan
Yes it's true I forget a code row "On Error Resume Next" immediately below declaration.
Now should work.
Sorry
Thank you grobnik, but it didn't work either to open AutoCAD.
Nevertheless, when I run the macro with AutoCAD already opened, the code gets correctly the AcadApplication Object.
I am thinking to do a walk around using the shell command
What do you think?
Hi,
No I never used shell or something like that could you try to fix reference library on Excel VBA,
That's I suggest it's working on my PC both if Autocad it's opened or not.
Good luck
Thank you norman.yuan, my computer has only one user in Windows 10 and it is the same user that runs Excel and AutoCAD in the same session i.e. I turn on the computer and log in Windows with the unique user, then I run Excel and from the ribbon I open Excel´s VBA editor. Finally I the run the macro directly from VBA editor or from Excel.
I don't know if there is a way to check where the problem is with this computer.
I have the same situation, I've used the code as mentioned including error handling
Public Function StartAutocad()
Dim acadApp As Object
Dim acadDoc As Object
On Error GoTo ErrorCatch
'Check if AutoCAD application is open. If is not opened create a new instance and make it visible.
Set acadApp = GetObject(, "AutoCAD.Application")
Set acadDoc = acadApp.ActiveDocument
If acadApp Is Nothing Then
Set acadApp = CreateObject("AutoCAD.Application")
acadApp.Visible = True
End If
Exit Function
ErrorCatch:
MsgBox "An Eror has Occured trying to opne the AUtocad apllication" & vbNewLine & _
"with error nr.: " & Err.Number & vbNewLine & _
"with error description: " & Err.Description
End Function
When autocad is running it does not give an error but if autocad is not running I get an error nr 429 description: axtive-x cannot create object
these are the references I use
Moderator edit: Put code in code window.
See if it helps using "Autodesk.Application.24.2". The following sub allows you to specify the version in case you have multiple versions installed.
'Sample usage for 2023
Set acadApp = GetAcad("24.2")
acadApp.Visible = true
'Sample usage for multiple
Set acadApp = GetAcad()
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
You can also check the registry to make sure that acad is installed correctly. Go to "Computer\HKEY_CLASSES_ROOT\AutoCAD.Application" and see if the class id's match the screenshot.
Hi Ed, thanks for the quick repsonse, I actually managed to get it running using :
Public Function StartAutocad()
Dim cadApp As AcadApplication
On Error GoTo ErrorCatch
If IsAppRunning("AutoCAD.Application") Then
MsgBox "Autocad is already running"
Else
Set cadApp = CreateObject("AutoCAD.Application")
End If
Exit Function
ErrorCatch:
MsgBox "An error has occured with error number " & iErrNo & vbNewLine & _
"and description :" & vbNewLine & _
sErrorText
End Function
Where IsAppRunning is a seperate function.
When you post code, please paste it into a code window by using the </> button. Choose VB as the language. It makes code easier to read and retains formatting.
I just noticed the difference in your first error handler and mine. You go to ErrorCatch, mine executes the next line. The problem is when you try GetObject and acad isn't running, then you get an error. So instead of going on to use CreateObject, you skip it to display a msg. In your second attempt, you avoid this logic by trapping the error in a separate function that just returns false instead of an error. But this over complicates things.
One advantage of mine, is that it returns the app as an AcadApplication object. That way the calling sub doesn't have to do it again. BTW, I edited it to make a small syntax change.
Can't find what you're looking for? Ask the community or share your knowledge.