How to open Autocad from excel and run a script, any tips would be helpful.

How to open Autocad from excel and run a script, any tips would be helpful.

Anonymous
Not applicable
4,394 Views
4 Replies
Message 1 of 5

How to open Autocad from excel and run a script, any tips would be helpful.

Anonymous
Not applicable

Hello, New to these forums as in it's my first post, been using the forums quite a bit however in the last week weeks to finely tweak my VBA macros etc... I'm trying to make an excel macro that will --> open auto cad --> run a script.... I have a few columns linked to another excel file and thats where the output goes too from running this script, just trying to streamline the process a bit more, thanks.

Accepted solutions (2)
4,395 Views
4 Replies
Replies (4)
Message 2 of 5

Alfred.NESWADBA
Consultant
Consultant
Accepted solution

Hi,

 

this is a short code-snippet to connect to AutoCAD or start AutoCAD.

Const AcAppClID As String = "AutoCAD.Application"

Public Sub startAcad()
   Dim tAcObj As AcadApplication
   
   On Error Resume Next
   
   'try if AutoCAD already is started
   Set tAcObj = GetObject(, AcAppClID)
   If tAcObj Is Nothing Then
      'AutoCAD is not running or not reacting, so we create a new process
      Set tAcObj = CreateObject(AcAppClID)
   End If
   If tAcObj Is Nothing Then
      'if that failed too something not correct
   Else
      tAcObj.Visible = True
   End If
   
End Sub

 

Make sure you have added the references to AutoCAD libs as this helps you during development.

You can later change all to "Object" and late-bound, but I would not run the development with that option.

 

20190121_193506.png

 

- alfred -

 

 

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

(not an Autodesk consultant)
Message 3 of 5

Anonymous
Not applicable

Thank you for your reply, i'm still having trouble however when it comes to using something alongthe lines of this

 

tAcObj.SendCommand EXTRACTLINE

 

If i use something very similar in autocad VBA it will work but I cannot get it working in this instance. I tried a few other ways of typing it but all to no avail. Any suggestions?

0 Likes
Message 4 of 5

Alfred.NESWADBA
Consultant
Consultant
Accepted solution

Hi,

 

>> If i use something very similar in autocad VBA it will work

Sorry no, that would not work in AutoCAD VBA as the AcadApplication object does not have a method "SendCommand".

Therefor you need a AcadDocument type, e.g.

   Dim tAcDoc As AcadDocument
   Set tAcDoc = tAcObj.ActiveDocument
   tAcDoc.SendCommand ("_LINE" & vbCr)

- alfred -

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

(not an Autodesk consultant)
0 Likes
Message 5 of 5

Anonymous
Not applicable

Awesome Alfred, Your code is 100% working, my problem now stemming from the command/script im trying to run is a custom script to extract line numbers and when im sending that specific command, it does nothing inside of ACAD.

0 Likes