Early Binding Versus Late Binding
Binding is another important automation concept. Two types of binding are available with automation components: early binding and late binding. With early binding, you create a reference to a component's type library. This notifies Access of all the library's objects, properties, methods, and events. With late binding, you instantiate objects at runtime without referencing them at design time. VBA doesn't know anything about the objects that you are creating until runtime.Most objects that you automate support early binding. You should use early binding whenever possible. Early binding has several benefits. Because each object's properties and methods are resolved at compile time, early binding is faster and more efficient. Furthermore, after you create a reference to a type library, all of the library's objects and their properties and methods are available via Intellisense. Finally, online help is available for any type libraries that you have referenced. This means, for example, if you have referenced Excel's library from Access, the process of placing your cursor on an object, property, or method and pressing F1 displays help for the selected item.Listing 22.1 provides an example of early binding. This code requires that a reference first be made to the Excel object library.
Listing 22.1 An Example of Early Binding
Sub EarlyBinding()
'Declare and instantiate an Excel Application object
Dim objExcel As Excel.Application
Set objExcel = New Excel.Application
'Set properties and execute methods of the object
With objExcel
.Visible = True
.Workbooks.Add
.Range("A1") = "Hello World"
End With
End Sub
CreateObject and GetObject
CreateObject and GetObject are required when using late binding. Because, with late binding, Access is not aware of the server application and its objects, properties, methods, and events, you cannot use a Dim statement and a Set statement to declare and instantiate the server application object. Instead, you must use Dim to declare a generic object variable. You then use a Set statement along with the CreateObject or GetObject function to work with the server object. The CreateObject function launches a new instance of the server object. The GetObject function is similar to CreateObject, but attempts to reference a running instance of the requested application. Furthermore, unlike the CreateObject function that receives only one argument as a parameter, the GetObject function receives an optional parameter with the name of the document you want to work with.Listing 22.2 provides an example of CreateObject and late binding.
Listing 22.2 Using the CreateObject Function to Create a Late-Bound Instance of Excel
Sub LateBinding()
'Declare a generic object variable
Dim objExcel As Object
'Point the object variable at an Excel application object
Set objExcel = CreateObject("Excel.Application")
'Set properties and execute methods of the object
With objExcel
.Visible = True
.Workbooks.Add
.Range("A1") = "Hello World"
End With
End Sub
NOTECalling GetObject doesn't determine whether the object is late- or early-bound. You can declare Dim objExcel as Excel.Application using GetObject, and the object will be early-bound.