In computer jargon, an error is referred to as a bug.  Errors can be grouped into three types based on when they occur—design time, compile time, or run time.
As the name implies, a design-time error occurs during the writing of code. Perhaps the nicest feature of the Visual Basic Editor is that it can be instructed to watch as we type code and stop us when we make a syntax error. This automatic syntax checking can be enabled or disabled in the Options dialog box shown in Figure 4-3, but I strongly suggest that you keep it enabled.
Notice also that there are other settings related to the design-time environment, such as how far to indent code in response to the Tab key.
To illustrate automatic syntax checking, Figure 4-4 shows what happens when we deliberately enter the syntactically incorrect statement
x == 5 and then attempt to move to another line. Note that Microsoft refers to this type of error as a compile error in the dialog box, and perhaps we should as well. However, it seems more descriptive to call it a design-time error or just a syntax error.
Before a program can be executed, it must be compiled, or translated into a language that the computer can understand. (This is not the place to go into a detailed discussion of compilation.) The compilation process occurs automatically when we request that a program be executed. We can also specifically request compilation by choosing the Compile Project item under the Debug menu.
If Word encounters an error while compiling code, it displays a compile error message. For example, the code in Figure 4-5 contains a compile-time error. In particular, the first line:
Dim doc As Document
defines a variable of type Document to represent a Word document. (I will discuss all of this at the appropriate time, so don’t worry about the details now.) However, the second line:
Set doc = ActiveDocument.Name
attempts to assign the variable doc not to the active document (which would be legal), but to the name of the active document. This error is not caught during design time because it is not a syntax error. It is only at compile time, when Word considers the statement in the context of the first statement, that the error becomes evident.
An error that occurs while a program is running is called a run-time error. Figure 4-6 illustrates a run-time error and its corresponding error message.
In this example, the code:
attempts to open a Word document that does not exist. Notice that this error message is actually quite friendly—not only does it describe the error in clear terms (the file could not be found), but it also offers some (albeit obvious) suggestions for eliminating the problem. (I find the second suggestion a bit amusing—you could not open the file you need because it could not be found; try opening a file that you don’t need! A more useful suggestion would have been to double-check the path for the requested file, but I certainly don’t want to complain, since this error message is far better than most.)
There is one more type of error that we should examine, since it is the most insidious kind of all. A logical error can be defined as the production of an unexpected and incorrect result. As far as Word is concerned, there is no error, because Word has no way of knowing what we intend. (Thus, a logical error is not a run-time error, in the traditional sense, even though it does occur at run time.)
To illustrate, the following code purports to compute the average of some numbers:
Dim x(3) As Integer Dim Ave As Single x(0) = 1 x(1) = 3 x(2) = 8 x(3) = 5 Ave = (x(0) + x(1) + x(2) + x(3)) / 3 MsgBox "Average is: " & Ave
The result is the message box shown in Figure 4-7. Unfortunately, it is incorrect. The penultimate line in the preceding program should be:
Ave = (x(0) + x(1) + x(2) + x(3)) / 4
(note the 4 in the denominator), since there are 4 numbers to average. The correct average is 4.25. Of course, Word will not complain because it has no way of knowing whether we really want to divide by 3.
Precisely because Word cannot warn us about logical errors, they are the most dangerous, because we think that everything is correct. Another problem is that logical errors can be very subtle, as illustrated by the following example from the Word object model.
Word VBA provides a very simple way to count the number of words in the currently selected text in a document. For instance, if I select the paragraph shown in Figure 4-8 and execute the code:
I will get a message box containing the number 57.
However, my count of the words in this paragraph is 51. The Word VBA help file neglects to mention that each punctuation mark is also considered a word! Thus, 3 periods plus 3 commas plus 51 words equals 57. The point here is that we need to be very circumspect in order to avoid logical errors.
Here is another example. How many words are selected in Figure 4-9? Answer: 2. Reason: Word counts each partially selected word as a whole word and considers the trailing spaces following a word as part of that word! Enjoy.
 In case you are interested in the origin of this word, the story goes that when operating the first large-scale digital computer, called the Mark I, an error was traced to a moth that had found its way into the hardware. Incidentally, the Mark I (circa 1944) had 750,000 parts, was 51 feet long, and weighed over 5 tons. How about putting that on your desktop? It also executed about one instruction every 6 seconds, as compared to over 200 million instructions per second for a Pentium!