Microsoft Office Tutorials and References
In Depth Information
Late Binding
Once you have established the proper reference, you can write a macro using early binding that will,
for example, open a Word document in Office 2010. Suppose you already have a Word document
named myWordDoc.docx that you keep in the path C:\Your\File\Path\. This macro will open that
document, using early binding:
Sub EarlyBindingTest()
Dim wdapp As Word.Application, wddoc As Word.Document
Set wdapp = New Word.Application
wdapp.Visible = True
Set wddoc = wdapp.Documents.Open(Filename:=”C:\Your\File\Path\myWordDoc.docx”)
End Sub
When you attempt to run this macro, you would immediately know if you
did not properly establish the Word 14.0 library reference, because you
would be prompted by a compile error message, as shown in Figure 26-2.
As you compose a macro using early binding, you will have the benefit
of VBA’s IntelliSense feature, where objects and properties pop up as you
type your code’s object references. And, macros with early binding run
faster than macros performing the same task with late binding, because a
reference has already been established to the target application’s objects,
methods, and properties.
figurE 26-2
If your macro runs without errors but you don’t see a Word document, or
you don’t even see Word on your task bar, it could be that you really did
create a new instance of Word, but it is not visible. In the Immediate window,
type Word.Application.Visible = True and press Enter .
So then, why would you ever not want to use early binding? Actually, there is a very good reason
why not: the referenced object (Word 14.0 in this example) must exist on the computer. If it does not
exist, an error will occur such as shown in Figure 26-2.
The concern is, unless you are composing your Office automation macros to be run on a system
that you know for a fact will (a) be installed with the target application, and (b) will have the
proper object library reference established in advance, chances are pretty good the macro will
fail using early binding. And with new Office versions being released every few years, when you
upgrade your Office version you will need to edit all the macros in which you utilized early
binding that will then be referring to an outdated earlier version.
late Binding
With late binding, you declare an object variable that refers to the target application, just as you
would with early binding. However, instead of setting the variable to a specific (in this case) Word
object, you create an object called a Word application.
 
Search JabSto ::




Custom Search