OFF: Overview of Visual Basic Editor Debugging ToolsLast reviewed: February 5, 1998Article ID: Q165517 |
The information in this article applies to:
- Microsoft PowerPoint 98 Macintosh Edition - Microsoft Word 98 Macintosh Edition - Microsoft Excel 98 Macintosh Edition - Microsoft Word 97 for Windows - Microsoft PowerPoint 97 for Windows - Microsoft Excel 97 for Windows
SUMMARY =======The Microsoft Visual Basic Editor provides a rich set of debugging tools that you can use to troubleshoot problems with your Visual Basic for Applications macro code. This article describes some general debugging techniques that you can use to debug Visual Basic for Applications code. NOTE: This article assumes that you are using the Visual Basic Editor to create macro code and that the code compiles correctly but does not perform as expected.
MORE INFORMATION ================Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:
http://www.microsoft.com/support/supportnet/refguide/default.aspYou can use several methods to debug Visual Basic for Applications code. Some of the commonly used techniques are described in this article.
USING A MESSAGE BOX TO DEBUG CODEThe most basic method to check the value of a variable at a specific step in the macro is to display that value in a message box. A message box that you use for debugging should be removed after you finish debugging. To use a message box for debugging, enter the MsgBox function code immediately after the line of code that uses the variable you want to check. For example, use the following syntax
MsgBox <NameOfVariable>where <NameOfVariable> is the name of the variable you want to check. The following sample macro displays a message box every time the value of variable "i" changes:
Sub MsgBoxCheck() Dim i For i = 1 to 5 MsgBox i Next i End SubFor more information about the MsgBox function, click the Office Assistant in the Visual Basic Editor, type "MsgBox" (without the quotation marks), click Search, and then click to view the "MsgBox Function" topic. NOTE: If the Assistant is hidden, click the Office Assistant button on the Standard toolbar. If Visual Basic for Applications Help is not installed on your computer, please see the following article in the Microsoft Knowledge Base:
ARTICLE-ID: Q120802 TITLE : Office: How to Add/Remove a Single Office Program or Component USING BREAKPOINTSYou can set a breakpoint to suspend a macro that is running at a specific statement in the macro. Typically, you set a breakpoint where you suspect a problem exists. You clear breakpoints when you no longer need them to stop the macro.
Setting BreakpointsTo set a breakpoint, use either of the following methods. Method 1:
Clearing Breakpoints
USING BREAK MODEWhen your code pauses, for example, when it encounters a breakpoint, the macro is in break mode. Break mode allows you to view the current condition of the macro. When a macro is in break mode, you can look at the values of your variables. You can also step through the code one line at a time to trace the logic of the macro.
Entering Break ModeOne method for entering break mode involves stepping through code from the beginning of the macro. To do this, follow these steps:
Exiting Break ModeTo exit break mode, click Reset on the Run menu. This ends the break mode session.
Using ToolTipsWhen you use break mode, ToolTips indicate the current value of a specified variable. To see a sample value displayed in a ToolTip, follow these steps:
NOTE: If the Assistant is hidden, click the Office Assistant button on the Standard toolbar. If Visual Basic for Applications Help is not installed on your computer, please see the following article in the Microsoft Knowledge Base:
ARTICLE-ID: Q120802 TITLE : Office: How to Add/Remove a Single Office Program or Component USING THE IMMEDIATE WINDOWThe Immediate window displays information that results from debug statements in your code or from commands that you type directly in the Immediate window. In the Immediate window, you can evaluate most valid Visual Basic for Applications statements.
Displaying the Immediate WindowTo display the Immediate window, click Immediate Window on the View menu or press CTRL+G.
Using the Debug ObjectUse the Debug object to print information in the Immediate window. You can use one method with the Debug object. This method is the Print method. The following example uses the Debug object and the Print method:
Sub DebugTest() ' Prints a string in the Immediate window. Debug.Print "This text is written in the Immediate window" ' Prints a floating point and an integer value. Debug.Print 2.3, 5 ' Outputs text and numbers. Debug.Print "Print the number " & 2 & "." End SubFor more information about the Debug Object, click the Office Assistant in the Visual Basic Editor, type "debug" (without the quotation marks), click Search, and then click to view the "Debug Object" topic.
Clearing the Immediate WindowThe text that is printed in the Immediate window using the Debug.Print statement or text that you type directly into the Immediate window is not cleared when the macro is finished. This behavior is by design. You can check the output that appears in the Immediate window after you run the macro. To clear the Immediate window, follow these steps:
Entering Commands in the Immediate WindowYou can type commands directly in the Immediate window. For example, to determine the name of an Office program, type the following text in the Immediate window
?Application.Nameand press ENTER. After you press ENTER, information that is similar to the following appears:
?Application.Name Microsoft PowerPointNOTE: You must be in break mode to enter statements in the Immediate window. For more information about the Immediate Window, click the Office Assistant in the Visual Basic Editor, type "immediate window" (without the quotation marks), click Search, and then click to view the "Use the Immediate Window" topic.
USING THE LOCALS WINDOWWhen you use break mode, the Locals window automatically displays the values and types for all declared variables in the current procedure. The following example uses the Locals window. To use the window, use the following steps:
USING THE WATCH WINDOWThe Watch window also allows you to monitor the values of your variables. Unlike the Locals window, values are not automatically populated; you must manually add the values that appear in the Watch window. NOTE: You can add values to the Watch window only when you are using break mode; therefore, you must be using break mode to use the following methods. To add a variable to the Watch window, use either of the following methods.
Method 1
Method 2
USING THE CALL STACKIn break mode you can use the call stack to display a list of currently active procedure calls. When you execute code in a procedure, that procedure is added to a list of active procedure calls. Each time a procedure calls another procedure, it is added to the list. When control is returned to the calling procedure, called procedures are removed from the list. Procedures called from the Immediate window are also added to the calls list.
USING CONDITIONAL COMPILATIONYou can use conditional compilation to selectively run blocks of code. The following sample macro uses conditional compilation:
Sub Test() #Const Debugging = 1 Dim Name As String: Name = "Nancy" ' If you are debugging, change the Debugging constant to 0. #If Debugging = 0 Then ' This debug statement is not executed unless the Debugging ' constant is equal to zero. Debug.Print Name #End If Name = "Kerry" End SubThe behavior of the #If...Then...#Else conditional compilation directive is the same as the If...Then...Else statement. However, code that is excluded during conditional compilation is completely omitted from the final executable file; so, using conditional compilation has no size or performance disadvantages.
|
Additional query words: 8.00 ppt8 vba vbe kbwordvba xlvbainfo 98 macppt
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |