AUGIWorld Magazine - January/February 2008 - (Page 18) AUTOCAD PROGRAMMING Getting Your Feet Wet with VBA Have you been ignoring this column due to the a phobia of ‘(‘ and ‘)’? Or maybe you are looking for a new challenge after you mastered being lost in parentheses. I know one thing, you are reading this article now. I’m going to teach you some VBA basics, beginning with the most basic information—what is VBA? Some of you already know, but for those who don’t, VBA is Visual Basic for Applications. (Were you thinking Visual Basic for AutoCAD® perhaps?) VBA has been around for many years and is an extension of the Visual Basic (or VB) programming language. Although both VBA and VB are based on the same syntax and the use of code modules and forms, they are slightly different. This article focuses only on VBA, and not VB. If you want to learn more about VB, click over to your favorite search engine and perform a search on it. Many popular Windows-based programs have VBA technology built into them. Some of the applications that support VBA are MS Word, MS Excel, Autodesk Inventor®, and AutoCAD starting with R14. VBA inside of AutoCAD has been a welcomed feature for those who develop applications professionally and even for those are just discovering their desire to write custom programs. Okay, so maybe programming is not something you are doing actively now, but if you are interested, this is the right place for you. While VBA is not the most powerful option in programming that you will ever encounter, it is one of the most approachable programming languages because it is easy to read and is very forgiving of the mistakes you might make as you use it. • Do not try to sit down and learn everything right away. Learning in small increments can often allow things to settle in for a bit for taking on more new concepts. • Most important, have fun; if it’s not fun, then it’s time for a break. Now go forth and learn to automate tasks in AutoCAD with VBA. Like any feature in AutoCAD, you need to know a command to start using a feature. The one command that you need to know is VBAMAN, also known as VBA Manager. The VBA Manager (see Figure 1) allows you to load and unload VBA project files. A project file contains the VBA code models and forms for a custom macro, and has a DVB file extension. Along with loading a project file, you can also load the Integrated Development Environment or IDE—more commonly known as the editor. The IDE is where you add and modify code modules and forms that are part of a project. A project file can contain code modules and forms. Code modules contain the instructions to manipulate a drawing or AutoCAD without the use of a dialog box, while a form allows you to create a dialog box that allows you to add interaction to a project through the use of buttons and other types of controls. Commands you need to know Starting the odyssey Just like you might go on a family vacation or head off to a convention, you always want to make sure you’re prepared. Before you start venturing down the path of VBA programming you should know the following. • Like anything new, it takes practice, so you will need to have patience. • Think of a task that you want to automate. Applying what you learn to a situation that is familiar to you will make it easier to learn VBA. Also, keep it to small tasks at first—don’t try to create the “Magic” button on Day One. • Learn to access the VBA help found in the Online Help system that comes with AutoCAD; if you prefer, you can snuggle up with a VBA book for those long, cold, winter nights ahead. Try Amazon or Barnes and Noble for books on VBA and AutoCAD. • No additional software is needed. If you have access to AutoCAD R14 or later you are good to go. Figure 1: VBA Manager dialog box 18 w w w. A U G I . c o m http://www.AUGI.com
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.