AutoCAD

VBA Lives On with AutoCAD 2016

6 Aug, 2015 By: Andrew G. Roe

It’s still possible to customize AutoCAD vertical products with Visual Basic for Applications (VBA).


Just as we can count on the annual release of a new version of AutoCAD software, I can count on hearing the same question pop up again and again: “I have some old VBA routines that worked great several years ago. Can I still run them in the latest version of AutoCAD?” With AutoCAD 2016 now released, the answer is still “Yes” — but with a few caveats.

As a refresher, Visual Basic for Applications (VBA) was introduced in AutoCAD Release 14, and enabled users to develop event-driven programs within AutoCAD sessions using a handy integrated development environment (IDE). VBA quickly gained popularity, as even novice programmers found they could write programs with graphical user interfaces (GUIs) and logical branching. Access to AutoCAD’s object model provided easy ways to create and modify entities, extract data from drawings, and adjust drawing settings and properties.

VBA remained a popular development platform for several years, but Microsoft stopped distributing VBA licenses in 2007, favoring its .NET framework for more flexibility in development, deployment, and Web services. Autodesk removed VBA from the standard AutoCAD interface in AutoCAD 2010 and essentially stopped supporting it, but VBA has maintained a pulse.

Autodesk unofficially supported VBA with a downloadable VBA enabler for post-2010 versions, and sure enough, continued the trend with AutoCAD 2016. The plugin has allowed many AutoCAD users to continue using VBA routines, albeit with occasional complications. Because the long-standing VBA 6 was a 32-bit application, it ran as a separate 32-bit process on 64-bit AutoCAD, sometimes resulting in unstable behavior. Microsoft then introduced VBA 7, compatible with both 32- and 64-bit platforms, improving stability and essentially giving VBA another lease on life. Autodesk now offers this version for AutoCAD 2014 and later versions.

While still not recommended for complex programs, you may find cases where VBA makes sense, particularly if you have some legacy code you’d like to keep using. Even though .NET is more powerful and scalable, it carries some baggage. Those simple 10-line routines in VBA could require 50 or more lines of code in .NET, along with the hassles of managing external references and developing code in a separate environment.

So if you’re sticking with VBA, here is a step-by-step example demonstrating how to automate a simple task in an Autodesk vertical product. In this case, the task is retrieving the end-point coordinates of a wall in AutoCAD Architecture. The process is slightly different for standard AutoCAD tasks, but most of the concepts still apply.

1. If you haven’t already done so, download the VBA Enabler applicable to your operating system (32 or 64 bits). The same enabler works in AutoCAD and vertical products.

2. Make sure AutoCAD is not running, and follow the installation instructions for the VBA Enabler.

3. Start AutoCAD Architecture, and make sure you have a wall object in the drawing.

4. Select the Manage tab on the AutoCAD ribbon, and click Visual Basic Editor. This opens the VBA Interactive Development Environment (IDE), as shown below.



5. In the VBA IDE, click Tools > References to open the References dialog box. Three references should already be checked when you open this: Visual Basic for Applications, AutoCAD Type Library, and OLE Automation.

1 2 3 


Add comment

Note: Comments are moderated and will appear live after approval by the site moderator.

Comments

Re: VBA Lives On with AutoCAD 2016
by: rexxitall
on:
October 31, 2015 - 9:00am
Really working again it starts by acad 2014. especially the 64bit version, which was bebore how to sy it icely - crap. But even with the actual version there is a lot to do by autodesk to make it as nice as it was in 2008. For exampe the help files are still incomplete (not understandable) and a lot of things are not tested by autodesk or still today a mess. But in summary good enougn and more fun then .net for daily work :) (Autodesk look at ACADSECTION -its only working if you use ACADOBJECT and not ACADSECTION - Help files are less then poor, Still today its impossible to generate views and section (especially sections acc. european standard) as with the flatshot command (flatshot can also not be used from VBA cause it want to have its *damned* dialog and so on. So there is a lot still to do improve, fix, enhance etc. Please, please do so ! :)
 
AutoCAD Tips!

Lynn Allen

In her easy-to-follow, friendly style, long-time Cadalyst contributing editor and Autodesk Technical Evangelist Lynn Allen guides you through a new feature or time-saving trick in every episode of her popular AutoCAD video tips. Subscribe to the free Cadalyst Video Picks newsletter and we'll notify you every time a new video tip is published. All exclusively from Cadalyst!

Follow Lynn on TwitterFollow Lynn on Twitter


Poll
Which device do you typically use to read Cadalyst.com content?
A desktop computer / tower workstation
A tablet
A smartphone
A laptop or mobile workstation
I regularly use both a desktop computer and a smartphone for this purpose
I regularly use another combination of devices for this purpose
I prefer to print out articles from the website and read them on paper
Submit Vote



Download Cadalyst Magazine Special Edition