Excel VBA – > Autocad Basics

Microsoft “deprecated” VBA and then Autodesk made the VBA module not a part of their massive install, convincing just about everybody it was no longer a viable platform to write any code. I am not going to write code for other people if i have to go sit at their computer and download and install a 100 mb file and do it on every release of autocad. But two things happened. Microsoft recanted. There must be a million people worldwide who code VBA excel. And it was never necessary to download the VBA module for autocad to run VBA code anyway. The download is just the code editor. The VBA objects are there. The code editor in excel works fine and in fact better because excel can hold data such as parameters, bills of material and cut-lists. For a few years now I have been making it a point not to download the autocad VBA module just to make sure other people could run my programs without adding anything.

To begin writing your Autocad VBA code in excel – start excel. Most of the time you have to add the developer tab to the ribbon. That is done with the File tab, Options, Customize Ribbon. Add the Developer tab. Start the Visual Basic editor. When you save an excel file with visual basic code, save it with XLSM file extension. In the visual basic editor, important first step, you must add a reference for excel to see autocad programming objects. This is under Tools, References. Look for Autocad Type Library. Move it up in the list as high as it will go.

2018-01-28_2.jpg

now you need a standard module to connect to autocad. insert a module, add two public variables at the top.

Public acadApp As AcadApplication
Public acadDoc As AcadDocument

start your sub procedure by typing
sub connect_acad()
and hit return

generally you will want to start autocad yourself the way you always do. if autocad is running the vba code to obtain a reference to it is

Set acadApp = GetObject(, "AutoCAD.Application")

notice we use our global variable. type this in to your sub procedure. I find this works fine but most online help shows it with a version dependent string like this.

Set acadApp = GetObject(, "AutoCAD.Application.22")
22 is the version that autocad is using to designate version 2018

to demystify this, what is GetObject, why is there a comma, where does this come from, open up the Object Browser. you can double click the word GetObject, right click, and pick Definition. if you dont find it that way, search the VBA library, top box, for GetObject –

The Object Browser shows its parent, VBA.Interaction and it shows the parameter list PathName comma Class. Right click on GetObject in the Object Browser and click Help to open MS online help.

2018-01-28_5.jpg

2018-01-28_3.jpg

If Autocad is not running, GetObject will return an error. The code to start autocad in VBA is CreateObject or New AcadApplication. They both work for me, just as the version independent and dependent string both seem to work equally well.

We need to handle the error, then deal with either autocad running or not. Autodesk has the logic and a file to do it here.

https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2018/ENU/AutoCAD-ActiveX/files/GUID-73EC319D-9D7D-43FF-91B7-78CF36284028-htm.html

this file is also in the Autodesk AutoCAD 2018: ActiveX Developer’s Guide, or whatever year you have, which is file acad_aag.chm somewhere in your autocad installation. find that and also acadauto.chm and copy them where you want them.

First we have to add the essential statement
acadApp.Visible = True
otherwise we will not see any autocad.

Autodesk has the commands and logic but not the best form. We have to make some improvements. The code to start autocad needs to be in its own sub procedure with the code to reference the active drawing. We want to call connect_acad once and we want to obtain a global variable for the active drawing.

When autocad opens, it probably opens to a blank drawing, or maybe its already open to a drawing, but we have to get that drawing into a VBA object. Generally I want it to be a blank drawing because I am going to use it. The connect_acad sub procedure will not run until we want it to, so it just gets the activedocument which is a property of AcadApplication. Those are our two global variables, the application and the active drawing, and active drawing is the main one we use.

Put connect_acad in your module as a stand-alone. Anytime you want to write a program in excel vba to draw or interact with autocad, the first thing in the program is

call connect_acad

at that point you have a global variable AcadDoc that you use for all of autocad’s vba objects.

here is the full acad_connect

Option Explicit
 
 Public acadApp As AcadApplication
 Public acadDoc As AcadDocument
 
 
 Sub Connect_Acad()
    
    On Error Resume Next
        
    Set acadApp = Interaction.GetObject(, "AutoCAD.Application")
    'Set acadApp = Interaction.GetObject(, "AutoCAD.Application.22")
    'both statements above behave without any discernible difference
    
    If Err Then
        Debug.Print "ERROR " & Err.Number
        Debug.Print Err.Description
        Debug.Print "starting autocad"
                
        Err.Clear
                      
         Set acadApp = New AcadApplication
         'Set acadApp = Interaction.CreateObject("AutoCAD.Application.22")
         'both statements above behave without any discernible difference
        
        'essential statement
        acadApp.Visible = True
        
        If Err Then
             MsgBox Err.Description
             Exit Sub
        End If
    End If
    
    Debug.Print "Now running " + acadApp.Name + " version " + acadApp.Version
         
    Set acadDoc = acadApp.ActiveDocument
        If acadDoc Is Nothing Then
            Set acadDoc = acadApp.Documents.Add
        End If
  
    If acadDoc.ActiveSpace = 0 Then
       acadDoc.ActiveSpace = 1
    End If
   
End Sub

in bricscad, try these (i dont have bricscad loaded currently but these worked a couple releases back)

Set acadApp = GetObject(, "BricscadApp.AcadApplication")

Set acadApp = New AcadApplication

using autodesk’s example to draw a single line would be modified like this. I made no other changes except adding connect_acad

Sub testline()

Call Connect_Acad

Dim lineObj As AcadLine
Dim startPoint(0 To 2) As Double
Dim endPoint(0 To 2) As Double
startPoint(0) = 1
startPoint(1) = 1
startPoint(2) = 0
endPoint(0) = 5
endPoint(1) = 5
endPoint(2) = 0

Set lineObj = acadDoc.ModelSpace.AddLine(startPoint, endPoint)
ZoomAll

End Sub

Using this method, any code on autodesk help website that uses Thisdrawing.object can be run from excel by adding connect_acad at the top and replacing the word Thisdrawing with AcadDoc.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s