Listing Early vs Late Binding

Sub WordEar1yBound()

Dim wd As Word.Application Dim doc As Word.Document

' Create new instance of Word Set wd = New Word.Application

' Add a new document Set doc = wd.Documents.Add

' Save & close the document doc.SaveAs "C:\testdoc1.doc" doc.Close

' Clean up Set doc = Nothing Set wd = Nothing End Sub

Sub WordLateBound()

Dim objWord As Object Dim objDoc As Object

' Create a new instance of Word

Set objWord = CreateObject("Word.App1ication")

' Add a new document

Set objDoc = objWord.Documents.Add

' Save & close the document objDoc.SaveAs "C:\testdoc2.doc" objDoc.Close

' Clean up

Set objDoc = Nothing Set objWord = Nothing End Sub

The WordEarlyBound procedure assumes that you have already set a reference to the Microsoft Word Object Library by selecting it in the References dialog box as shown in Figure 14.8. You can always tell when early binding is being used by looking at the variable declarations. By setting a reference to the Microsoft Word Object Library, you allow VBA to have knowledge of the Word object model. As a result, you can declare variables using the specific object associated with each variable rather than declaring each variable using the generic Object object.

The WordLateBound procedure doesn't require you to set a reference. On the flip side, the variables must be declared as Object objects. Further, because VBA has no knowledge of the properties and methods associated with the variables, the VBE can't provide any help in the form of Auto List Members or Auto Syntax Checking. Writing code when you are using late bound objects is old-school. You either memorize object libraries or else spend a lot of time in the help files looking up properties, methods, and method parameters.

An Automation Example: Presentation Automation

It's time to break out the role playing hats and take a look at an example in which automation may be a good solution. For this example, you're a financial analyst at a large retailer—Bullseye Corporation. One of your many tasks is to prepare a presentation each month for your boss who then presents the data to upper management. This report details sales information such as same-store sales comparisons, revenue per store, and other key indicators.

Most of the presentation involves financial data that you prepare in Excel. Because your company has invested in Microsoft Analysis Services to facilitate rapid analysis in Excel, it only takes you a matter of minutes to gather the data required for the report.

Was this article helpful?

0 0

Post a comment