VBA – Early and Late Binding

What exactly are early and late bindings? This is what MSDN tells us about it:

The Visual Basic compiler performs a process called binding when an object is assigned to an object variable.

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.

By contrast, 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.

You should use early-bound objects whenever possible, because they 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. 

binding

So, after so much copy+paste, let’s give a couple of examples for early and late binding and finish the article 🙂 :

Here comes the early binding:

Here comes the late binding 🙂 :

Pretty much this is all. The difference is that in early binding you get intellisense and compiling time bonus, but you should make sure that you (and whoever uses your application) has added the corresponding library.

Tagged with: ,