Early Late Binding

A brief explanation of 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 if your method calls are valid for any given object.

    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 2000 is version 9, Office XP, version 10, Office 2003 is version 11, and Office 2007 is version 12.

    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 actually set the object reference, it's the declaration that counts. So you can use CreateObject with an object declared as Outlook.Application.

    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 also need to set a reference to the object library in order to set the early-bound declaration.

Update 3/10/2009: I've backed off on this position quite a bit. What I do now is add object library reference, code my application early bound, then convert everything to late bound code before using it in a "real" application (or posting it on my blog). See Why I prefer late bound code and Take advantage of Intellisense when writing late bound code for details on just how to do that. That way I get the benefits of early bound AND late bound code.

Common Object Libraries

    Here are the more popular object libraries I see getting used in code on the web and in the newsgroups:

  • 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 (since both Word and Excel have a Range object).

    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.

Site last updated March 17, 2010 @ 10:12 pm; This content last updated January 12, 2010 @ 3:19 pm