Microsoft Office Tutorials and References
In Depth Information
Which One Is Better?
If you use late binding, you do not use Tools References to set a reference because you do not
know which Word object library version will be on a user’s machine. Instead, you use code to
create the object. The following macro named LateBindingTest accomplishes the same task as the
EarlyBindingTest , by opening a specific Word document:
Sub LateBindingTest()
Dim WdApp As Object, wddoc As Object
Set WdApp = CreateObject(“Word.Application”)
WdApp.Visible = True
Set wddoc = WdApp.Documents.Open(Filename:=”C:\Your\File\Path\myWordDoc.docx”)
End Sub
In a nutshell, when you declare a variable As Object and set it as CreateObject , VBA doesn’t
know whether the object is a cell, a worksheet, a Word application, or any other object. The code
goes through a series of tests behind the scenes until it finds the correct application for the use
intended by your code. That’s the essential reason why late binding takes longer to execute.
Which one is Better?
For my money, even with moderately sized macros, the extra seconds of run time due to late binding
make up for the headaches of trying to accommodate every version of your target Office application,
from 2000 through 2010. You will find that the VBA skills you are acquiring will lead to
composing macros that others will use, and you’ll never know what Office versions are installed on users’
systems. People have varying opinions on the merits of early versus late binding, so consider the pros
and cons of both methods to decide which approach is best for you.
Try iT
In this lesson, you compose a macro using late binding that opens a Presentation file in PowerPoint.
lesson requirements
For this lesson, you first create a PowerPoint presentation, name that file PowerPointExample1,
and save it into the folder path C:\Your\File\Path\. You’ll compose a macro that will open the
PowerPoint file, taking into consideration that the Office version is unknown, so the late binding
method will be utilized.
step-by-step
1.
Open a new workbook and press Alt+F11 to go to the Visual Basic Editor.
2.
From the menu at the top of the VBE, click Insert Module.
3.
In the module you just created, type Sub OpenPowerPoint and press Enter. VBA will
automatically place a pair of empty parentheses at the end of the Sub line, followed by an empty
line, and the End Sub line below that. Your macro will look like this so far:
Sub OpenPowerPoint()
End Sub
Search JabSto ::




Custom Search