I’m Not Seeing Red, But I’m Seeing Red
The above ex ample was fairly simple. It was one line, so we knew where
the prob lem was; it had to be in that line. Many of your pro cedures will be more
complicated. If you have a pro cedure with two or three or ten or twenty lines,
you won’t know where the prob lem is. One small error in the mid dle of a pro ce-
dure might cause your but ton to do noth ing. You should be able to catch the er-
rors that the VBA Ed itor turns red because they will be red, but the ones that
don’t turn red are harder to find.
Now we need some way to fig ure out which line is the prob lem for those er-
rors that don’t turn red. While VBA has some tools to help you with de bugging,
these tools are not al ways the best choice. That is because of the dis tinction be -
tween Slide Show View and Edit View. When you are in the VBA Ed itor,
PowerPoint is gen erally sitting in Edit View. Remember that most of our proce-
dures are made to work in Slide Show View (anything that starts with
MsgBox_._MsgBox'>ActivePresentation.SlideShowWindow
is only go ing to work in Slide
Show View).
To solve this problem, you can use
MsgBox
.
MsgBox
is a sim ple (read that
as “hard to mess up”) command that pops up a message. Add a few
MsgBox
commands to your code with informative messages that tell you what you are ex -
pecting and where you are in the code. For ex ample, you might put a
MsgBox
at
the beginning of the procedure:
MsgBox("En
ter
ing the pro
ce
dure AddStar.")
When you run the pro cedure, if you don’t even get a message that pops up
to say, “En tering the procedure AddStar,” you know the prob lem prob ably is not
in the procedure (un less it is in the
Dim
state ments in the procedure be cause the
158 De bug ging Tips
first
Do'stlaringiz bilan baham: |