VBA
Discuss AutoCAD ActiveX and VBA (Visual Basic for Applications) questions here.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Excel VBA send autocad commands

3 REPLIES 3
Reply
Message 1 of 4
RCBmstg007
10182 Views, 3 Replies

Excel VBA send autocad commands

I am getting better at using VBA for Excel. I was able to find a script macro that could launch Autocad. Can you send commands to the command line of Autocad from an Excel VBA? For example, I want autocad to import an XML. Or have a user select an object to export. We are using Civil 3D. (Referring to a pipe network.)

I don't think there is a method to record mouse clicks and keyboard strokes to a VBA then have it replay from Excel VBA or so.
3 REPLIES 3
Message 2 of 4
Anonymous
in reply to: RCBmstg007

Once you create an Autocad object you have access to all of the methods and properties of Autocad. This is done in VBA and is not limited to command line functions. If you have something specific, search this and other AutoCAD VBA forums.

Dim acad As Object
Set acad = AutoCAD.Application
Message 3 of 4
dgorsman
in reply to: RCBmstg007

Civil3D is its own beast of a program, highly dependant on sets of pre-existing conditions to function properly.  Trying to drive it from the outside sounds like a diagnostic nightmare waiting to happen.  I would instead investigate making the program work inside Civil3D (it has its own API) and consuming Excel files as a data source.  Unless you are only planning to use Excel as a program/macro "host", in which case it makes even more sense to put it *all* into Civil3D.

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


Message 4 of 4

Hi,

 

>> Can you send commands to the command line of Autocad from an Excel VBA?

At least yes, you can use the function "SendCommand" of the document-object and start commands in AutoCAD.

 

Minimalistic Code (no cleanup, no excessive error handling):

 

Public Sub startCommandInAcad()
   Dim tAcadApp As AcadApplication
   Set tAcadApp = getAcadApp
   If (tAcadApp Is Nothing) Then
      Call MsgBox("No AcadApplication found")
   Else
      If (tAcadApp.ActiveDocument Is Nothing) Then
         Call MsgBox("No current Drawing found in AutoCAD-Application")
      Else
         On Error Resume Next
         tAcadApp.ActiveDocument.SendCommand ("_-LANDXMLOUT" & vbCr & "C:\TEMP\ExpFile.XML" & vbCr)
         If Err.Number <> 0 Then
            Call MsgBox("Error occured during 'SendCommand'" & vbNewLine & Err.Description)
         End If

         On Error Goto 0
      End If
   End If
End Sub

Private Function getAcadApp() As AcadApplication
   Dim tRetVal As AcadApplication
   On Error Resume Next
   Set tRetVal = GetObject(, "AutoCAD.Application")
   On Error GoTo 0
   Set getAcadApp = tRetVal
End Function

BUT: as, also mentioned by dgorsman, a tool running within Civil3D let's you access more functions resulting in better control, especially if something fails.

The SendCommand is something quite critical as you don't get any feedback. E.g. if Civil3D blocks the command you sent (e.g. for command LANDXMLOUT it might interrupt because there is no data in the file to export ... or a file with that used to export already exists ...) you don't get a feedback like an exception or a return value like you get from functions. After that Civil3D might wait for the next command or it might be unrepsonsive to COM as it has a "overwrite yes/no"-dialog open.

 

If you need Excel to start Civil3D and start functions in it create an app that can be loaded into Civil3D, make it COM-compatible so you can access the functions/properties within your Civil3D-internal app from your VBA-Code in Excel.

 

BTW: there is a special forum for Civil3D-customization >>>here<<<.

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost