Microsoft KB Archive/212673

= WD2000: Troubleshooting Variables and Data Types =

Article ID: 212673

Article Last Modified on 10/11/2006

-

APPLIES TO


 * Microsoft Word 2000 Standard Edition

-



This article was previously published under Q212673





SUMMARY
This article offers suggestions for using variables and data types in Microsoft Visual Basic for Applications in the programs listed at the beginning of this article.



MORE INFORMATION
You can implicitly declare a variable in Visual Basic by using an assignment statement, such as the following: Set oPar1 = ActiveDocument.Paragraphs(1) All variables that you implicitly declare this way are of type Variant, which require more memory than other data types. Therefore, your program will run more efficiently if you declare variables explicitly and with a specific data type.

Doing this can also reduce the incidence of naming-conflict errors and spelling mistakes. A naming conflict occurs when you try to create or use an identifier that was previously defined in the project. In some cases, naming conflicts generate error messages such as the following:

Ambiguous name detected

-or-

Duplicate declaration in current scope

Undetected naming conflicts can produce bugs in your code, especially if you do not explicitly declare all variables before first use.

If you do not want Visual Basic to make implicit declarations, you can place the Option Explicit statement in a module before any procedures. This statement requires you to explicitly declare all variables within the module. If a module includes the Option Explicit statement, a compile-time error occurs when Visual Basic encounters a variable name that you have not declared, or that you have spelled incorrectly.

You can set an option in your Visual Basic programming environment to automatically include the Option Explicit statement in all new modules. To do this, follow these steps:
 * 1) Open the Microsoft Visual Basic Editor by pointing to Macro on the Tools menu in Word (or another Office program) and clicking Visual Basic Editor.
 * 2) In the Visual Basic Editor, click Options on the Tools menu.
 * 3) On the Editor tab, click to select the Require Variable Declaration check box.
 * 4) Click OK.

The following is an example of the correct syntax for variable declaration: Dim oVar1, oVar2 As Object This may appear to declare two Object variables; however, in this case, oVar1 is implicitly a Variant (the default data type), and oVar2 is explicitly an Object. The following syntax produces two Object variables: Dim oVar1 As Object Dim oVar2 As Object

-or-

Dim oVar1 As Object, oVar2 As Object Explicit data types have another advantage when you use the Visual Basic Editor Auto List Members feature. This feature produces automatic lists of collection-members at the insertion point while you type. If the editor knows the data type of your variable, it can provide these auto lists. If the data type of your variable is implicit (and therefore of type Variant), the editor cannot provide auto lists.

For example, if you explicitly declare a data type and assign a value for your variant, such as in the following arguments Dim oPar1 as Paragraph Set oPar1 = ActiveDocument.Paragraphs(1) you receive an auto list as soon as you then type: oPar1 If you do not explicitly declare the variable's data type, the auto list does not appear.

This feature can help you to avoid incorrect variable usage by displaying only the collection members, methods, or properties that are relevant to the data type of your variable. Incorrect variable usage can produce error messages such as the following:

Object doesn't support this property or method

-or-

Method or data member not found

To turn on the Auto List Members feature, follow these steps:
 * 1) Open the Microsoft Visual Basic Editor by pointing to Macro on the Tools menu in Word (or another Office program) and clicking Visual Basic Editor.
 * 2) In the Visual Basic Editor, click Options on the Tools menu.
 * 3) On the Editor tab, click to select the Auto List Members check box.
 * 4) Click OK.

For more information about dimensioning variables, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type declaring variables in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

