Early & Late Binding

Many of the examples on this site require early binding. Early binding means you are referencing object libraries at compile time, rather than run-time. If you set references to object libraries, you will have access to the properties and methods of that program during the code-writing phase, which is an immense benefit for anyone, even experienced programmers. It makes writing code much easier because you can use the Intellisense feature to auto-complete code, which avoids many syntax errors and cuts down on time spent coding. Plus it makes your code much faster, since VBA doesn't have to waste time at run-time figuring out what you are trying to do.

To set a reference to an object library, open your favorite Office program and press Alt-F11 to access the VB Editor, then Click Tools>References and select the appropriate library. For Excel, choose "Microsoft Excel x.0 Object Library", or for Outlook choose "Microsoft Outlook x.0 Object Library". The "x" stands for your version; Office XP is version 10, Office 2003 is version 11. So if you were setting a reference to the Excel 2003 Object library, choose "Microsoft Excel 11.0 Object Library."

Object Libraries

Here are some examples to illustrate the difference when coding.

Early bound object reference:

Dim objOL As Outlook.Application
Set objOL = New Outlook.Application

Late-bound object reference:

Dim objOL As Object
Set objOL = CreateObject("Outlook.Application")

Actually, it doesn't really matter how you set the object reference, it's the declaration that counts. If you declare an object as Object, it will always be late-bound, because Excel won't know what kind of object you are going to assign to it until you actually do so. Declaring an object as a specific type will mean it is early-bound, but of course you will need to set a reference to the object library in addition to the declaration.


Common Object Libraries

  • Microsoft CDO 1.21 Library
  • Microsoft Scripting Runtime
  • Microsoft HTML Obect Library
  • Microsoft Internet Controls
  • Microsoft Word x.0 Object Library
  • Microsoft Excel x.0 Object Library
  • Microsoft Outlook x.0 Object Library

Remember if you set a reference to any/all of these, you should make a habit of fully qualifying all of your object references. For example, instead of Dim Rng As Range use Dim Rng As Excel.Range to make sure the object model you are working with knows for sure what library you are using.

The only time I recommend you not set explicit references and use early binding is when you don't know what type of object you are working with. For example, when you are cycling through your Outlook Inbox and processing mail items, you might run into a meeting request. If you wrote Dim item As Outlook.MailItem instead of Dim item As Object, your code would fail when it tries to treat the meeting request like a message. If you declare the object reference as a generic Object, you can always check each one in an IF statement such as:

For Each item In objFolder.Items
    If TypeName(item) = "MailItem" Then
        ' do processing on messages only here
    End If
Next item


That way you are covered no matter what type of item you reference.

LAST UPDATED: April 12, 2008