Understanding Object Binding in VBA - Late Binding
(Page 3 of 4 )
The second method of instantiating objects is known as late binding. In late binding, a reference to the object is not provided to the application; instead, instantiation is made through an object's IDispatch interface at the time of execution. This is the type of object instantiation used by the Windows Script Host environment, which doesn't provide support for object references.
Since the object's type library is not processed prior to execution, the object's exposed interfaces are not available. This means two things as far as we're concerned. First, we cannot declare a variable of that type. Second, we will not be able to instantiate the object by its interface.
Since the variable we'll be using for our object cannot be declared by the object type, we'll simply declare it as a generic type Object. Without an interface by which to instantiate it, we'll forgo the New syntax and use VBA's CreateObject method instead. To do that you'll need to know the ProgId for the COM object you wish to use. In this case, the ProgId for the Microsoft Excel Object Library is "Excel.Application."
Dim objExcel As Object
Set objExcel = CreateObject("Excel.Application")
Those familiar with the Windows Script Host will find this syntax very familiar, as it is quite close to its WSH equivalent.
Instantiating objects in this way negates any need to have references in your application, but it also provides a few other distinct advantages. Let's take a moment to examine the pros and cons of both early and late binding so you know when to use each of them.
Next: An object lesson >>
More Visual Basic.NET Articles
More By Nilpo