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. 


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

The early binding:

In order to make the Early binding possible, we had to add the Microsoft Word Object Library to the VBEditor (Extras>Libraries):

Then, we were able to use the intellisense of the word object:

The late binding:

Pretty much this is all. The difference is that in early binding you get intellisense and compiling time bonus. It saves adding a library by the developer (some 45 seconds).

Tagged with: , , ,