Late Binding

With late binding, you create an object that refers to the Word application before linking to the Word library. Because you do not set up a reference beforehand, the only constraint on the Word version is that the objects, properties, and methods must exist. In the case where there are differences, the version can be verified and the correct object used accordingly.

The disadvantage of late binding is that Excel doesn't know what is going on—it doesn't understand that you are referring to Word. This prevents the tips from appearing when referencing Word objects. Also, built-in constants are not available and, when compiling, Excel cannot verify that the references to Word are correct. After the program is executed, the links to Word begin to build, and any coding errors are detected at that point.

The following example opens and makes visible an existing Word document:

Sub WordLateBinding()

Dim wdApp As Object, wdDoc As Object

Set wdApp = CreateObject("Word.Application")

Set wdDoc = wdApp.Documents.Open(ThisWorkbook.Path & _

"\Chapter 16 - Automating Word.doc")

wdApp.Visible = True

Set wdApp = Nothing

Set wdDoc = Nothing

End Sub

An object variable (wdApp) is declared and set to reference the application (CreateObject("Word.Application")). Other required variables are then declared (wdDoc) and the application object is used to refer these variables to Word's object model.

0 0

Post a comment