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 professionals 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/
You can use several methods to debug Visual Basic for Applications code.
Some of the commonly used techniques are described in this article.
The 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 Sub
For 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
You 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.
To set a breakpoint, use either of the following methods.
Method 1:
1. Click anywhere in the line of code at which you want the macro to halt.
A flashing pointer appears.
2. On the Debug menu, click Toggle Breakpoint (or press F9).
A breakpoint is defined for that line of code (provided that a
breakpoint was not previously defined for the line of code).
Method 2:
1. Right-click the line of code that contains the breakpoint.
2. On the shortcut menu, point to Toggle, and then click Breakpoint.
When you run this code, the macro halts on the first line of code that contains a breakpoint. Note that you can use more than one breakpoint in a macro.
1. Click anywhere in the line of code that contains the breakpoint you want
to remove.
2. On the Debug menu, click Toggle Breakpoint (or press F9). The breakpoint
is removed from the line of code.
NOTE: Breakpoints are not saved with your code. So, you must configure
breakpoints for every debugging session.
When 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.
One method for entering break mode involves stepping through code from the beginning of the macro. To do this, follow these steps:
1. Click the line of code that contains the first instruction of the macro.
2. On the Debug menu, click Step Into.
An arrow appears to the left of the first line of code.
3. On the Debug menu, click Step Over.
This step advances the arrow to the next instruction. If the instruction
that is highlighted is a procedure (function, sub, or property), using
the Step Over command executes the procedure as a unit without stepping
through the code.
To step through a procedure line by line, click Step Into.
NOTE: The Debug menu commands are also available on the Debug toolbar.
To exit break mode, click Reset on the Run menu. This ends the break mode session.
When 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:
1. In a Visual Basic module, type the following sample subroutine:
Sub Test()
Dim Name As String
Name = "Kerry"
Name = "Nancy"
End Sub
2. On the Debug menu, click Step Into (or press F8) to step through the
subroutine.
Click Step Into again until the Name = "Kerry" line is highlighted, and
then move the pointer over the Name variable.
A ToolTip appears with the following text:
Name = ""
3. Press F8 to execute the line of code that assigns the Name variable.
The Name = "Nancy" line should be highlighted.
4. Move the pointer over the Name variable. A ToolTip appears with the
following text:
Name = "Kerry"
For more information about break mode, click the Office Assistant in the
Visual Basic Editor, type "Step" (without the quotation marks), click
Search, and then click to view the "Step Into, Step Over, and Step Out
Commands (Debug Menu)" 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
The 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.
To display the Immediate window, click Immediate Window on the View menu or press CTRL+G.
Use 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 Sub
For 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.
The 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:
1. Click the Immediate window.
2. On the Edit menu, click Select All.
3. On the Edit menu, click Clear or Cut.
You 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.Name
and press ENTER.
After you press ENTER, information that is similar to the following appears:
?Application.Name
Microsoft PowerPoint
NOTE: 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.
When 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:
1. In a Visual Basic module, type the following code:
Sub LocalsTest()
Dim x As Long, y As Long, z As Long
x = 1
y = 2
z = x + y
End Sub
2. Step through the code until the "y = 2" line is highlighted. To do
this, follow these steps:
a. Press F8 or click Step Into on the Debug menu.
b. Press F8 three times so that the y = 2 line is highlighted.
3. If the Locals window is not visible, click Locals Window on the View
menu.
4. When the "y = 2" line is highlighted, the Locals Window should resemble
the following table.
Expression Value Type
---------------------------------------
x 1 Long
y 0 Long
z 0 Long
5. Press F8 again to execute the y = 2 line. View the Locals window.
The expression y has the value 2. If you execute the next line (z = x +
y), z is assigned the value 3.
6. Before you execute the next line, change the value of y to 5. To do
this, follow these steps:
a. In the Locals window, click the value of y (2).
b. Change the value to 5 and press ENTER.
c. Press F8 again to execute the "z = x + y" line.
The Locals window should resemble the following table.
Expression Value Type
---------------------------------------
x 1 Long
y 5 Long
z 6 Long
The 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.
1. Right-click the variable you want to add to the Watch window. (You
can click any occurrence of the variable.)
2. On the shortcut menu, click Add Watch.
The Expression field should display the name of the variable that you
clicked in step 1.
You can also add expressions to the Watch window.
1. On the Debug Menu, Click Add Watch.
2. Type the expression you want to evaluate and click OK.
For more information about adding expressions to the Watch Window, click the Office Assistant in the Visual Basic Editor, type "watch expressions" (without the quotation marks), click Search, and then click to view the "Add a Watch Expression" topic.
In 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.
You 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 Sub
The 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
Keywords : kbcode kbprg DBugDebugW DBugErrhand kbfaq
Version : WINDOWS:97; MACINTOSH:98
Platform : MACINTOSH WINDOWS
Hardware : MAC x86
Issue type : kbinfo
Last Reviewed: April 3, 1999