Common WordBasic Errors

The WordBasic errors described in this section are errors that prevent a macro from running. These errors occur when an instruction breaks one of the rules that govern the way you can use the WordBasic language. Word displays an error message as soon as it encounters a line containing an error. If you have the macro open in a macro-editing window, Word highlights the offending line in red. Every WordBasic error message box contains a Help button. When you choose the Help button, Word provides suggestions about what may have caused the error.

Note

Word does not highlight all the errors in a macro at once. If you have many typos or syntax errors in your macro, you will get an error message each time you run your macro, until all the errors are corrected.

Syntax Error

This is by far the most common error message. The following are some of the causes of syntax error messages.

A missing quotation mark

A string must be enclosed by quotation marks. An error is generated if one or both are missing.

A missing, misplaced, or extra comma

Each argument for a statement or function must be separated by a comma. A missing, misplaced, or extra comma generates an error.

A missing period

Each argument for a statement that corresponds to a dialog box must begin with a period. For example, .FileName is an argument for the FileOpen statement.

A missing parenthesis at the end of a function

A function is always followed by an opening and closing parenthesis. A syntax error is generated if one or both are missing.

A missing reserved word

Some WordBasic instructions include more than one reserved word. For example, an If¼Then¼Else instruction must include the Then reserved word.

A reserved word name conflict

WordBasic generates a syntax error if you create a variable name that matches a reserved word. For example, a variable named "Then" conflicts with the reserved word Then, which is part of the If¼Then¼Else statement.

Type Mismatch

This error occurs when an instruction requires a particular data type but doesn't get it. Because there are only two data types in WordBasic — strings and numbers — the error means that a string was used when a number was required, or vice versa. In the following example, pet is meant to be a string variable, but it is missing a dollar sign ($), so Word interprets it as a numeric variable and generates an error:


pet = "poodle"            'Should be: pet$ = "poodle"

This error is also generated when you provide a function with an argument of the wrong type. Here is an example:


a$ = Str$("4")            'Should be: a$ = Str$(4)

Wrong Number of Parameters

This error is generated when a statement or function has too many or too few arguments (also known as "parameters"). Here are some examples:


a$ = Chr$()                'Chr$() requires an argument
a$ = Str$()                'Str$() requires an argument
a$ = Selection$(1)        'Selection$() does not take an argument
MsgBox                'MsgBox requires an argument
ChDir                'ChDir requires an argument

Unknown Command, Subroutine, or Function

This error message usually means that you have misspelled a function or statement name. It can also occur if you omit the dollar sign ($) from a function that returns a string. Each of the following instructions generates this error message:


MgsBox "Hello"            'Should be: MsgBox "Hello"
EditFnd "muskrat"        'Should be: EditFind "muskrat"
quote$ = Chr(34)            'Should be: quote$ = Chr$(34)

In addition, this error can occur if an instruction that should be two words is typed as one — EndDialog, for example, instead of End Dialog (though both EndIf and End If are accepted).

Undefined Dialog Record Field

This error message is displayed if you misspell the argument for a statement that corresponds to a dialog box, as in the following example:


EditFind "sasquatch", .WhleWord = 1            'Should be: .WholeWord = 1

The error also occurs if you include an argument that doesn't belong, as in the following:


EditFind "skunk", .WholeWord = 1, .Musk = 1    '"Musk" is not valid

Bad Parameter

This message is displayed if the value for an argument is outside the range of accepted values, as in the following example:


ChangeCase 50                        '"50" is too large a value

Duplicate Label

This error occurs if the macro includes two subroutines or user-defined functions with the same name. It also occurs if you create two Goto labels that have the same name.