Skip to main content

Early and Late Binding

What is binding?

Binding is a process of matching function calls written by the programmer to the actual code (internal or external) that implements the function. It is done when the application is compiled, and all functions called in code must be bound before the code can be executed. The Visual Basic compiler performs a process called binding when an object is assigned to an object variable.

Early binding

An object is early bound when it is assigned to a variable declared to be of a specific object type. Early bound objects allow the compiler to allocate memory and perform other optimizations before an application executes.

To use early binding on an object, you need to know the exact object that will be used at design time.

For example, if you wanted to Automate Microsoft Office Excel using early binding, you would add a reference to the "Microsoft Excel 16.0 Object Library":

  1. In Excel, press ALT + F11 to open Visual Basic Editor.
  2. From the VBE menu bar, select Tools > References – to open the "References - VBAProject" dialog.
  3. Check the box next to "Microsoft Excel 16.0 Object Library" to include in your workbook.
  4. Click "OK".

And then declare your variable as being of the type "Excel.Application" From then on, all calls made to your object variable would be early bound:

' Set reference to 'Microsoft Excel 16.0 Object Library' in
' the Tools > References dialog.

' Declare the object as an early-bound object
  Dim oExcel As Excel.Application

  Set oExcel = CreateObject("Excel.Application")

' The Visible property is called
  oExcel.Visible = True

This method works great most of the time, but what if you don't know the exact object you will be using at design time? For example, what if you need to talk to multiple versions of Excel, or possibly to an "unknown" object altogether?

Late binding

An object is late bound when it is assigned to a variable declared to be of type Object. Objects of this type can hold references to any object, but lack many of the advantages of early-bound objects.

For example, the following code fragment declares an object variable to hold an object returned by the CreateObject function:

' No reference to a type library is needed to use late binding.

' Declare the object as a late-bound object
  Dim oExcel As Object

  Set oExcel = CreateObject("Excel.Application")

' The Visible property is called
  oExcel.Visible = True

As you can see, the rest of your code is the same. The only difference between early binding and late binding (in terms of the code you write) is in the variable declaration.

Which form of binding should I use?

The answer to this question depends as much on the design of your project as anything else. Microsoft recommends early binding in almost all cases. However, there may be reasons for choosing late binding.

Advantages of Early Binding

  • Early-bound objects allow the compiler to make important optimizations that yield more efficient applications.
  • Early-bound objects are significantly faster than late-bound objects and make your code easier to read and maintain by stating exactly what kind of objects are being used.
  • Early binding enables useful features such as automatic code completion and Dynamic Help because the Visual Studio integrated development environment (IDE) can determine exactly what type of object you are working with as you edit the code.
  • Early binding reduces the number and severity of run-time errors because it allows the compiler to report errors when a program is compiled.

Advantages of Late Binding

Late binding is still useful in situations where the exact interface of an object is not known at design-time. If your application seeks to talk with multiple unknown servers or needs to invoke functions by name (using the Visual Basic 6.0 CallByName function for example) then you need to use late binding.

Late binding is also useful to work around compatibility problems between multiple versions of a component that has improperly modified or adapted its interface between versions.

Leave a comment

Your email address will not be published. Required fields are marked *

Format your code: <pre><code class="language-vba">place your code here</code></pre>