What is Early and Late Binding in Excel automation using VBA. Watch the video below:
The process of connecting VBA to external or internal files containing ready-made functions which can be used for automation without rewriting the code is known as binding. Our VBA code will contain references to objects, properties and methods to the accessed functions and these will be seamlessly integrated into our code. These references can be done at design time while writing the code also known as Early Binding. Let us take the example of extracting data from web-pages and placing the details in our Excel worksheet. We will need to reference the Internet Explorer web browser and other functionalities, for example.
In the Visual Basic for Applications Editor we will perform the following steps:
Go to in the Tools|References and check the following functions libraries
1. Micrsoft Internet Controls
2. Microsoft HTML Object Library
These function libraries have been created by Microsoft and we will be reusing them.
The advantages of Early Binding are:
The process is defined once in design-time.
It produces faster execution of automation.
The user gets Intellisense support.
The disadvantage of Early binding are compatibility issues. The user with whom we share our workbook may not have the same version of the functions libraries.
Here is an example of early binding as also described in the video tutorial:
Dim wb As InternetExplorer
Set wb = CreateObject(“internetexplorer.Application”)
wb.Visible = True
Let’s take an example of late binding:
Dim oExcel As Object
Set oExcel = CreateObject(“Excel.Application”)
oExcel.Visible = True
In Late Binding, the connection between our code and the functions libraries is made during run time. As we can see in the above example, the only difference between early binding and late bindiing is in the variable declaration. We declare an object variable as “Object”, telling Visual Basic for Applications to use late binding.
The major advantage of Late Binding is that the code picks up the most recent Object Type Library available on the running machine and compatibility issues are avoided.
The main disadvantage is the speed of execution of the automation as compared to Early binding and no Intellisense support is available. But not everybody seems to support this view.