Share this Page URL

Chapter 22. Automation: Communicating wi... > Early Binding Versus Late Binding - Pg. 778

Automation: Communicating with Other Applications 778 Executing Methods Properties refer to the attributes of an object, and methods refer to the actions you take on the object. Methods are the verbs that apply to a particular object type. Here's an example: objExcel.Workbooks.Add This code uses the Add method to add a workbook to the Excel object. 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 com- ponent'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 pos- sible. Early binding has several benefits. Because each object's properties and methods are re- solved 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. Example 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 state- ment 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 appli- cation. Furthermore, unlike the CreateObject function that receives only one argument as a pa- rameter, 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.