Debugging in VBA (a HowTo)

Debugging in VBA (an HowTo)With the new compatibility of VBA with Autocad, some of you might want to explore the capacities of VBA. I think you should as the VBA language is strong, quick and in my opinion transparent. I’m a self thought VBA programmer, and especially in my first years I developed my capacities slow. I now believe I should have known more about debugging in VBA. I wrote this to help starters: Knowing how to debug one’s code is vital for sound programming. Frustration is probably unavoidable. Knowing how to debug will surely help avoid frustration too. As I failed to find a good tutorial on the web to point to, I decided to start this thread with my own experiences. I had to be short. Please feel free to make additions to it.----------------------------------------The purpose of debugging is to have detailed insight into what your code is doing with your variables and objects at any time during the execution of your code. ----------------------------------------1. Option ExplicitBefore you start I recommend you have the Option ‘Require Variable Declaration’ set in the Options dialog of the Visual Basic Editor (VBE). At the top of your module sheets the statement Option Explicit will now appear. This option will demand from you that you declare your variables before you use them. This will help you in preventing errors.This option is responsible for the error message “Variable not defined”. It points to variable names that are unknown. You might have forgotten to declare it. Other causes might range from a serious mistake to a simple typo.2. Compile your codeIn the Visual Basic Editor (VBE) you have the menu option Debug – Compile. I suggest you get used to compile your code before execution. It will find and highlight syntax mistakes in your code. Beware: The error messages are often difficult to interpret, especially for the beginner. If you do not understand the syntax mistake that VBE reports to you, try to run your code as it is not required to compile your code beforehand. Your code will run without being compiled and as a consequence stumble on the syntax mistakes your code contains while executing.3. Your code halts with an error messageWhen an error message pops up, you code is halted at the location the error is found. Select ‘Debug’ and use any of the techniques presented here to get to understand the mistake your code contains. 4. MsgBoxIf you have a variable that you suspect of causing mistakes, you’ll want to know what the value of the variable is during execution. You’ll include a ‘MsgBox variable’ statement in your code. Using the function MsgBox has the advantage that you can have the BricsCad window on top while you execute your code, and still be reported about the value of the variable. To prevent the execution of the rest of your code include an ‘Exit Sub’ statement right after it.There are more advanced techniques than MsgBox, all accessible in the Visual Basic Editor (VBE).5. Debug.PrintSimilar to the MsgBox is the statement ‘Debug.Print variable’. It writes the actual value of the variable into the Immediate Window that is part of the VBE. Ctrl+G will bring up that window. The execution of the code is not halted. The generated values can be evaluated after the execution of the macro.6. Setting BreakpointsAny line of code can be set as a breakpoint (except for comment lines). You click in the left margin of the code in the VBE to set or unset a breakpoint. A big round dot appears in the margin. ‘F9’ is a short-cut to toggle the breakpoint. Execution of your code will automatically halt at this place and you’ll be able to have a detailed look into the current state of your variables and objects and their values resp. properties. You’ll use for this the Locals Window and/or the Watch Window. You can continue with the execution by pressing F5. Pressing F8 can be used to go step by step through the code. You can set more than one breakpoint. Breakpoint can be very useful if set inside loops. 7. The Locals WindowThe Locals Window displays all your declared variables and objects in the current procedure, their values and their type. The Locals Windows is automatically updated. Declared variables might still be empty, containing “” or ‘Nothing’ etc. Objects might contain properties that are not yet initialised (). Arrays and Objects can be expanded to reveal all their members resp. properties. It is a great source of information. Not only can you check all variables and objects at any time during the execution of your code, you also might explore the different object’s properties collections. Note: If you have ‘Type Mismatch’ errors, do review the Type of your variables here. One simply cannot perform math operations on a variable of the type String. Search your VBA Help file for ‘Type Conversion Functions’.8. The Watch WindowThe Watch Window is similar to the Locals Window but contains only ‘watch expressions’. A watch expression is a user-defined expression. Right-click on a variable in your code and select ‘Add watch’ from the pop-up menu to include the variable in the watch window. Longer selections of your code can be added the same way. You have great freedom in defining your own statements. You can watch for instance one specific property of your object (ActiveDocument.ActiveLayer.Name) instead of having it available in the expandable tree of properties that the ActiveDocument object contains. You can also anticipate here: You can defining the conversion of your String variable to a Double operations as a watch (CDbl(String)), before you apply that conversion in your code. You can check on a certain match (ActiveDocument.ActiveLayer.Name = String). And so on.Watch statements have an option that makes them capable of triggering a break.9. The Immediate WindowDuring break mode the Immediate Window can be used as well. You type ‘Debug.Print ActiveDocument.ActiveLayer.Name’ for that value to be printed in the immediate window. ’? ActiveDocument.ActiveLayer.Name’ is the same. Operations can be executed here as well. 10. The Object BrowserFinally: If you want to know more about an object or its properties, or before you use new objects or properties, you might want to know some more about these items. Much information is available in the Object Browser (press F2). Properties and Methods are structures per class and besides information on Type, some limited information is available. Note: The Object Browser reveals Methods that can be applied to Objects. This can be very important information. The method Add for instance is not available in the object AcadLayer, but the method Delete can be applied to an AcadLayer object. To Add a Layer one needs to use the object AcadLayers. (ActiveDocument.AcadLayers.Add "newname").

Comments

  • Yes, the VBA programming enviroment is great. I decided against LISP because of this. I also held off on a large programming project for over a year, waiting for the VBA-AutoCAD compatable version to be released.The reason for waiting is that I needed good documnentation and a tutorial. When the VBA became AutoCAD compatable, then I could use any VBA for AutoCAD book to help me along.Joe Dunfee

This discussion has been closed.