Harry's Code Class (September 2007)

24 Sep, 2007

VBA vs. Visual LISP: The Debate Continues

VBA is far more versatile, but Visual LISP goes with AutoCAD
like jam on toast. How does one choose?

In last month's edition of Harry's Code Class, I asked which programming language you would like me to focus on most. The current tally in the Cadalyst Hot Tip Harry -- Help! Discussion Forum has Visual LISP and VBA almost even, with only a few requests for Object ARX and .NET. That makes sense, because Visual LISP and VBA are supplied with AutoCAD. (If you'd like to add your two cents' worth to the discussion, log on to the forum today. Use is free, but requires registration.)

Both Visual LISP and VBA have their respective strengths and weaknesses, as mentioned by the forum respondents. Some of the comments included with the votes have been interesting, and I want to take the opportunity to expand on a few of them in this edition. Such a discussion will enlighten many who are very new to the world of AutoCAD customization.

Benefits of VBA
One of the strongest arguments for working with VBA is that you can use it to customize other programs as well -- primarily Microsoft Office products such as Word, Excel, Outlook, and Access. VBA is a variation of the BASIC language, which has been a part of the Microsoft family for decades in one fashion or another. As the name implies, BASIC (Beginners All-purpose Symbolic Instruction Code) is an easy language to read and learn. It was created to teach computer programming skills and has a similar syntax to many other programming languages. BASIC has evolved over the decades; the most current variation is Visual BASIC from Microsoft.

VBA (Visual Basic for Applications) is a specialized version of Visual BASIC, designed to run inside an application as a customization tool. The difference in using VBA from one environment to another relates to the data specific to the host application. For example, when customizing AutoCAD, programs manipulate data that are primarily graphics. In a word processing system such as Microsoft Word, you can use VBA to tune paragraphs, sentences, and words. But the basics of VBA remain the same, so you can carry over VBA programming skills to different environments. This is a very persuasive argument for learning the tool, if you plan to program applications beyond AutoCAD.

The developer environment -- where you do the programming -- is another selling point of VBA. In AutoCAD, you type VBAIDE at the Command line to activate the IDE (interactive developer environment), which provides built-in tools that make programming easier. (OK, it's not so easy at first. To the beginner, the display can be a confusing menagerie of little windows and icons, similar to AutoCAD or any other robust Windows application. Learning VBA requires some stubborn practice. You can take a VBA class or read some books, but the majority of VBA books and classes are not AutoCAD-specific. The best place to learn VBA for AutoCAD is in the Help system for AutoCAD VBA.)

So what do I find cool about the VBA developer environment? My favorite feature is the automatic typing. AutoCAD and other applications have large libraries of objects with which you must become familiar -- both as the objects themselves, and in terms of how to customize the function of each one using VBA -- which takes a while to learn. Once you understand the objects themselves (scouring the AutoCAD Help is the best place to start for that), you can launch into the customization. As you type source code related to an object, VBA assists by automatically displaying associated options and Help. Once you get rolling, the automatic typing is a great productivity tool.

A few forum visitors pointed out another benefit of VBA: debugging tools. After you write a program, the next step is to test and fix it -- also known as debugging. The VBA developer environment lets you run your program with set stopping points, called break points, that pause the program so you can inspect how things are going.

When you test a program and it encounters a break point, the line of code displays, highlighted, in the VBAIDE. At this point you can instruct VBA to show you the values of variables and enter VBA statements to be immediately executed. Looking at variable values can help you determine if something is not being correctly set. Typing immediate statements for execution will allow you delve more deeply into objects and try things that you might want to add to the code. (Note that Visual LISP offers a similar feature, as described later.)

When I write VBA code and have questions about an object?s values or actions, I will generally write code up to the point of getting the object into a local variable. Then I will put in a break point, run the program as is, and display the immediate window to poke around the object data and methods until I find what I'm after. This approach can be extremely helpful when wandering alone into the underdocumented back alleys of AutoCAD.

Visual LISP: Tried and True Tool
Visual LISP has its own selling points, as noted by several respondents on the forum. Visual LISP is a dialect of LISP that runs only inside AutoCAD. Its top draw is how well and how easily it works to customize AutoCAD. This tight-knit relationship means you can write code to produce customized commands for AutoCAD that will run much like the built-in AutoCAD commands, making them easier to use. The same is true of Visual LISP functions you can create for menu systems, dashboards, and toolbars. This makes Visual LISP-based features typically easier to implement and support because operators are already comfortable with the interface.

In the Visual LISP developer environment, VLIDE, you'll find a debugging tool similar to that of VBAIDE. You can set break points that stop the program evaluation process, allowing you to probe the symbols and run expressions. Just as is true with the VBA version, this debugging tool is exceptionally useful when searching for things of interest inside open objects and for figuring out why a program is not working as expected.

The legacy aspect of the Visual LISP language is something not to be overlooked. Visual LISP evolved from AutoLISP and has been an AutoCAD programming tool since the mid-1980s. Examples, books, and classes are focused on AutoCAD-based programming. Cadalyst?sGet the Code! tip library from "Hot Tip Harry" is composed of mostly AutoLISP and Visual LISP code. So, if you're looking for the best tool for customizing inside AutoCAD, the choice is pretty clear: Visual LISP.

I must qualify my last statement by saying that I have been customizing AutoCAD since the earliest version for various applications and disciplines. I have extensive experience programming AutoLISP and subsequently Visual LISP, which is my first tool of choice for most uses. But to be fair, I should mention that I am big fan of BASIC (and its dialects, including VBA) and have been writing BASIC programs since the mid-1970s. BASIC is a great tool for creating quick utility programs and simple end-user applications. I like both!

One or the Other, but not Both
So, do VBA and Visual LISP work well together? Sadly, no. Visual LISP is capable of loading and starting VBA macros. VBA can load and start Visual LISP expressions. But the two do not communicate and synchronize well -- that is, they don?t share data and are not always aware of one another's activities.

A couple of programming tricks exist that can help these two languages work together. The first involves breaking programs into smaller modules. Using such a strategy, you could have a Visual LISP function start up an application, then hand over control to a VBA macro for some user interaction. When the user interface is completed, VBA could then run the appropriate Visual LISP function to do the task. Complicated? Yes. This approach is not for beginners. The main reason to take on a programming challenge like this would be to modernize an older application using VBA?s superior user interface capabilities.

The missing ingredient is how to pass data back and forth between the modules. My quick-and-dirty solution is to write data to a file, then read it in and update it as the program chain progresses. This solution comes from the old days of programming when a series of programs was often needed to complete a complicated task using limited memory. Interestingly, you can come across situations in today?s modern computer environments where legacy concepts of tape drives and card reader programming are still useful. Then again, LISP originated in the 1950s, and if you think about it, is considered one of the oldest computer languages. Thus it is only appropriate to resort to such tactics when using these tools!

Which is Better? You Tell Me
Visual LISP or VBA? In many regards, the choice comes down to personal preference. Remember to vote for your pick at the Hot Tip Harry -- Help! Discussion Forum. The focus of future editions of Harry Code Class newsletter will depend greatly on your input. Use the forum to ask your programming questions, as well. If you are involved in customizing AutoCAD and cannot find an answer you need, try posting your query on the forum. Who knows, what you need might show up as the topic of a future newsletter!

Until the next time, keep on programmin'.

Related Resources

Programming Basics Articles

Programming Basics Books