Professional Documents
Culture Documents
Debugging
Invariably, you will encounter errors in your code. Design-time
and compile-time errors are relatively easy to deal with because
Word helps us out with error messages and by indicating the of-
fending code. Logical errors are much more difficult to detect
and to fix. This is where debugging plays a major role. The Word
IDE provides some very powerful ways to find bugs.
https://www.oreilly.com/library/view/writing-word-macros/9781565927254/ch04s06.html 1/12
11/04/2021 Debugging - Writing Word Macros, Second Edition [Book]
Try a very simple example, with which you should follow along
on your PC. If possible, you should arrange your screen as in
Figure 4-10. This will make it easier to follow the effects of the
code, since you won’t need to switch back and forth between
the Word window and the Word VBA window. The code that we
will trace is shown in Example 4-1. (Note that lines beginning
with an apostrophe are comments that are ignored by Word.)
Sub test()
End Sub
https://www.oreilly.com/library/view/writing-word-macros/9781565927254/ch04s06.html 2/12
11/04/2021 Debugging - Writing Word Macros, Second Edition [Book]
the tracing process. (You can also choose Step Into from the De-
bug menu.)
I will now examine some of the tools that Word provides for de-
bugging code.
https://www.oreilly.com/library/view/writing-word-macros/9781565927254/ch04s06.html 3/12
11/04/2021 Debugging - Writing Word Macros, Second Edition [Book]
Tracing
The process of executing code one line at a time, as I did in the
previous example, is referred to as tracing or code stepping. Word
provides three options related to tracing: stepping into, step-
ping over, and stepping out of. The differences between these
methods refers to handling calls to other procedures.
Sub ProcedureA()
ActiveDocument.Content.Text = "To be or not to be"
Call ProcedureB
ActiveDocument.Content.Bold = True
End Sub
Sub ProcedureB()
Set para = ActiveDocument.Paragraphs(1)
para.Style = "Heading 1"
End Sub
https://www.oreilly.com/library/view/writing-word-macros/9781565927254/ch04s06.html 4/12
11/04/2021 Debugging - Writing Word Macros, Second Edition [Book]
Call ProcedureB
ActiveDocument.Content.Bold = True
https://www.oreilly.com/library/view/writing-word-macros/9781565927254/ch04s06.html 5/12
11/04/2021 Debugging - Writing Word Macros, Second Edition [Book]
Step Over is similar to Step Into, except that if the current state-
ment is a call to another procedure, the entire called procedure
is executed without stopping (rather than tracing through the
called procedure). Thus, for instance, stepping over the line:
Call ProcedureB
ActiveDocument.Content.Bold = True
Run To Cursor
https://www.oreilly.com/library/view/writing-word-macros/9781565927254/ch04s06.html 6/12
11/04/2021 Debugging - Writing Word Macros, Second Edition [Book]
Watching Expressions
It is often useful to watch the values of certain expressions or
variables as we trace through a program. Word provides several
ways to do this.
https://www.oreilly.com/library/view/writing-word-macros/9781565927254/ch04s06.html 7/12
11/04/2021 Debugging - Writing Word Macros, Second Edition [Book]
https://www.oreilly.com/library/view/writing-word-macros/9781565927254/ch04s06.html 8/12
11/04/2021 Debugging - Writing Word Macros, Second Edition [Book]
The Watch window shows all of the watches that have been set.
A watch is a variable or expression that is placed in the Watch
window. Word automatically updates the expressions in the
Watch window after each line of code is executed and acts ac-
cording to the type of watch defined, as described next.
To add a watch, choose Add Watch from the Debug menu. This
will produce the dialog box shown in Figure 4-13.
https://www.oreilly.com/library/view/writing-word-macros/9781565927254/ch04s06.html 9/12
11/04/2021 Debugging - Writing Word Macros, Second Edition [Book]
https://www.oreilly.com/library/view/writing-word-macros/9781565927254/ch04s06.html 10/12
11/04/2021 Debugging - Writing Word Macros, Second Edition [Book]
ABOUT O’REILLY
Teach/write/train
Careers
Community partners
Affiliate program
Submit an RFP
Diversity
https://www.oreilly.com/library/view/writing-word-macros/9781565927254/ch04s06.html 11/12
11/04/2021 Debugging - Writing Word Macros, Second Edition [Book]
SUPPORT
Contact us
Newsletters
Privacy policy
Take O’Reilly online learning with you and learn anywhere, anytime on your phone
and tablet.
View all O’Reilly videos, Superstream events, and Meet the Expert sessions on your
home TV.
© 2021, O’Reilly Media, Inc. All trademarks and registered trademarks appearing on oreilly.com are the
property of their respective owners.
Terms of service • Privacy policy • Editorial independence
https://www.oreilly.com/library/view/writing-word-macros/9781565927254/ch04s06.html 12/12