Early Binding

When you declare object variables as specific object types, Visual Basic uses early binding. Early binding is also known as compile-time binding. This means that Visual Basic associates your object variable with the Automation object in the period during which the procedure source code is translated to executable code. The general syntax looks like this:

Dim objectVariable As Application.ObjectType

In the above syntax, Application is the name of the application as it appears in the Object Browser's Project Library drop-down list (for example, Word and Excel). ObjectType is the name of the object class type (for example, application, document, workbook, worksheet).

The following declarations result in early binding:

Dim mydoc As Word.Document

Dim mydoc As Excel.Worksheet

Early binding allows you to take full advantage of many of the debugging tools that are available in the Visual Basic Editor window. For example, you can look up external objects, properties, and methods with the Object Browser. Visual Basic Auto Syntax Checking, Auto List Members, and Auto Quick Info (all discussed in Chapter 2) can help you write your code faster and with less errors. In addition, early binding allows you to use built-in constants as arguments for methods and property settings. Because these constants are available in the type library at design time, you do not need to define them. The handy built-in syntax checking, IntelliSense features, and support for built-in constants aren't available with late binding. Although VBA procedures that use early binding execute faster, some very old Windows applications can only use late binding.

Note: In order to use early binding, you must first establish a reference to the object library (see the following section). Use early binding when you are certain that your users will have the referenced type libraries installed on their machines.

0 0

Post a comment