How to Debug VBA Code in Word’s Editor
🔍 WiseChecker

How to Debug VBA Code in Word’s Editor

If you write macros in Word, you have likely encountered code that does not run correctly. The VBA editor in Word includes tools to step through code, inspect variables, and find logic errors. This article explains how to use the debugger built into the Visual Basic Editor. You will learn to set breakpoints, watch values, and handle runtime errors.

Key Takeaways: Debugging VBA Macros in Word

  • Alt+F11: Opens the Visual Basic Editor from Word
  • F9 on a code line: Toggles a breakpoint to pause execution at that line
  • F8 (Step Into): Executes one line of code at a time for detailed inspection
  • Ctrl+G (Immediate Window): Runs quick VBA commands and checks variable values while debugging
  • Debug > Compile VBAProject: Catches syntax and compile errors before running the macro

How the VBA Debugger Works in Word

The Visual Basic Editor in Word is the same environment used by other Microsoft Office applications. It contains a built-in debugger that can pause macro execution, show variable contents, and let you run code line by line. To use the debugger, you must first open the VBA editor by pressing Alt+F11 while Word is active. The editor has its own menu bar, toolbar, and several windows: the Project Explorer, the Code window, the Immediate window, and the Locals window.

Before debugging, ensure your macro security settings allow VBA code to run. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings and select Enable all macros. This setting is for testing only; disable it after debugging to prevent malicious code from running.

The debugger does not fix errors automatically. It shows you where the error occurs and what values variables hold at that moment. You must interpret that information to correct the code.

Breakpoints — Pausing Code at a Specific Line

A breakpoint tells the debugger to stop execution at a particular line. To set a breakpoint, open the macro in the Code window, click the gray margin to the left of the line you want to pause at, or press F9. A red circle appears in the margin and the line is highlighted in red. When you run the macro, execution stops at the breakpoint before that line runs. You can set multiple breakpoints in one macro.

The Locals Window — Watching Variables

When execution is paused, the Locals window (View > Locals Window) shows every variable in the current procedure. It lists the variable name, its current value, and its type. As you step through code, the values update automatically. This window is the fastest way to see whether a variable contains the data you expect.

The Immediate Window — Running Commands on the Fly

The Immediate window (Ctrl+G or View > Immediate Window) is a command line inside the VBA editor. While paused, you can type a VBA statement and press Enter to execute it. For example, type ?Selection.Text to print the selected text in Word. Use this window to change a variable value or test a function without restarting the macro.

Steps to Debug a VBA Macro in Word

Follow these steps to debug a macro that is not producing the expected result.

  1. Open the Visual Basic Editor
    In Word, press Alt+F11. The VBA editor opens. If your macro is in a module, double-click the module name in the Project Explorer to show its code.
  2. Set a breakpoint at the first suspicious line
    Scroll to the line where you suspect the problem starts. Click the left margin or press F9. A red dot appears.
  3. Run the macro
    Press F5 or click the Run button on the toolbar. The macro runs until it reaches the breakpoint. At that point, the editor window becomes active and the breakpoint line is highlighted in yellow.
  4. Step through the code one line at a time
    Press F8 to execute the current highlighted line. Each press runs one line and moves the highlight to the next line. Watch the Locals window to see how variable values change after each step.
  5. Inspect variable values
    Hover your mouse over a variable name in the code. A tooltip shows its current value. Alternatively, type ?variableName in the Immediate window and press Enter.
  6. Change a variable value if needed
    In the Immediate window, type variableName = newValue and press Enter. This overwrites the variable’s value for the remainder of the debugging session.
  7. Continue or stop debugging
    To resume normal execution, press F5. To stop debugging entirely, click the Reset button on the toolbar or press Ctrl+Shift+F8.

Common Mistakes and Limitations in VBA Debugging

Breakpoint does not turn red when I press F9

This happens when the code window is not focused or when the line is a comment or a blank line. Click inside the code window first, then press F9. Breakpoints can only be set on executable lines.

Macro stops but the Locals window is empty

The Locals window only shows variables that are declared in the current procedure. If you declared variables at the module level, they appear only when the macro is paused inside a procedure that uses them. Open the Locals window by pressing Ctrl+Alt+V, L if it is not visible.

Error message appears but I cannot see the line number

By default, VBA shows a dialog with the error description but not the line number. To see the exact line, click Debug in the error dialog. The editor highlights the offending line in yellow. You can then inspect variables and step forward.

Word freezes when I run the macro with breakpoints

This usually means the macro entered an infinite loop. Press Ctrl+Break (or Ctrl+Pause on some keyboards) to interrupt execution. The editor will stop at the current line. Check loop conditions and counters in the Locals window.

Compile error prevents the macro from running

Before debugging, always compile your code. Go to Debug > Compile VBAProject. If errors exist, the editor highlights the first problematic line. Fix the syntax, missing references, or undeclared variables, then compile again until no errors appear.

VBA Debugging Tools in Word: Breakpoints vs Stop Statements vs Debug.Print

Item Breakpoint (F9) Stop Statement Debug.Print
How to set Click margin or press F9 Type Stop in the code Type Debug.Print variable in the code
Pauses execution Yes, at that line Yes, at the Stop line No, execution continues
Shows output No output No output Prints to Immediate window
Removed automatically No, must remove manually No, must delete the line No, must delete the line
Best used for Pausing at a specific location Conditional pauses inside loops Logging values without stopping

Use breakpoints for most debugging sessions. Use Stop when you want a breakpoint that is saved with the code file. Use Debug.Print to monitor values over many iterations without interrupting the macro flow.

Now you can open any macro in Word, set breakpoints, step through code, and inspect variables using the Immediate and Locals windows. Start by compiling your project to catch syntax errors, then place a breakpoint at the first line that might cause trouble. Press F8 to step through each line and watch how your variables change. For complex macros, add Debug.Print statements to log intermediate values to the Immediate window without stopping execution.