An easy way to see the difference between early and late binding is to look
at the way objects are declared in code. Code that implements late binding
includes the "As Object" definition when dimensioning the variable. Code that implements early binding declares the variable as a specific object type at the point at which the variable is dimensioned. For example, "As Excel.Application" would define a variable as an object, and specifically, an Excel object.
Example of Late Binding
Late binding means that the object type is declared at the same time that the object is created rather than at the time that the variable is defined. For example, to automate Microsoft Excel with late binding, you could use the following code:
Function Test()
'Declare an object variable to hold an object reference.
'"Dim <VariableName> As Object" indicates late binding.
Dim ExcelSheet As Object
Set ExcelSheet = CreateObject("Excel.Sheet")
End Function
Example of Early Binding
Early binding means that, as you dimension an object, the object class type is specified within the same statement rather than defining a generic object type. For example, to automate various Microsoft Excel objects with early binding, you could use the following code:
Function Test()
'Requires a reference to the Microsoft Excel 9.0 Object Library.
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.WorkSheet
Set xlApp = CreateObject("Excel.Application")
Set xlBook = CreateObject("Excel.Workbook")
Set xlSheet = CreateObject("Excel.Worksheet")
End Function
Early and Late Binding with Microsoft Graph
With earlier versions of Microsoft Graph, early binding was not possible. The only way to automate a Microsoft Graph object was with late binding, as shown in the following example:
Private Sub Command0_Click()
Dim GraphObj As Object
Set GraphObj = Forms!MyForm!MyGraph.Object
End Sub
In Microsoft Graph 9.0, late binding is no longer a requirement but an option. You can now early bind to a Graph object, as illustrated in the following sample code:
Private Sub Command0_Click()
Dim GraphObj As Graph.Chart
Dim GraphApp As Graph.Application
Set GraphObj = Forms!MyForm!MyGraph.Object
Set GraphApp = GraphObj.Application
End Sub
Microsoft Graph is not a stand-alone software program, as is Microsoft Access, Microsoft Excel, or Microsoft Word. Graph can only be used within other programs; for example, Graph can be used within Microsoft Access reports. Therefore, it is not possible to use the
CreateObject function to instantiate a graph object, as was illustrated earlier when instantiating Excel objects. The only way by which you can automate a graph object is when that object first resides within another host application. The sample code above assumes that the graph object is being used in an Access form. The graph object instantiated in the sample code above is based on a graph that was first inserted on an Access form.