The Immediate Window

The Immediate window (see Figure 3-8) has two main functions. First, we can send output to this window using the command:

	Debug.Print

For instance, the code:

	Debug.Print ActiveWindow.Selection.Range.Text

will print whatever text is currently selected in the active window to the Immediate window. For instance, Figure 3-8 shows what happened when I selected the words “whatever text is currently selected” in the previous sentence and then ran the procedure shown in the Code window of Figure 3-8. We can see the result in the Immediate window. This provides a nice way to experiment with different code snippets.

The Immediate window

Figure 3-8. The Immediate window

The other main function of the Immediate window is to execute commands. For instance, by selecting some text in the active document, switching to the Immediate window, and entering the line shown in Figure 3-9, the selected text will be boldfaced (after pressing the Enter key to execute the code).

The Immediate window: command execution

Figure 3-9. The Immediate window: command execution

The Immediate window is an extremely valuable tool for debugging (finding errors in) a program, and you will probably use it often (as I do).

Get Writing Word Macros, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.