VBA Lives On with AutoCAD 20166 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.
About the Author: Andrew G. Roe
In her easy-to-follow, friendly style, long-time Cadalyst contributing editor 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!