PDA

View Full Version : Early?late Binding?



clvestin
12-21-2005, 07:33 AM
From Excel, I prepare a small Word memo

Public Sub Memo()
Dim a As Integer, b As Integer, c As Integer, d As Integer
Dim WordApp As Object
Dim myrange1 As Object, myrange2 As Object
Dim wordDoc As Object
Set WordApp = CreateObject("Word.Application")

SaveAsName = ThisWorkbook.Path & "\" & "wren" _
& Format(Date, "mmddyyyy") & ".doc"

With WordApp
.documents.Add
Set wordDoc = WordApp.activedocument
With .Selection
.Font.Size = 14
.Font.Bold = True
.paragraphformat.Alignment = 1
.typetext Text:="MY REPORT"
.typeparagraph
.typeparagraph
.Font.Size = 12
.paragraphformat.Alignment = 0
.Font.Bold = False
.typetext Text:="Date:" & vbTab & _
Format(Date, "mmmm d, yyyy")


Obviosly not the whole routine, but as Word is set as an object, this would be an example of late binding.

so when i use


Public Sub Memo()
Dim a As Integer, b As Integer, c As Integer, d As Integer
Dim WordApp As Word.Application
Dim myrange1 As Object, myrange2 As Object
Dim wordDoc As Word.Document
Set WordApp = New Word.Application
SaveAsName = ThisWorkbook.Path & "\" & "wren" _
& Format(Date, "mmddyyyy") & ".doc"

With WordApp
.documents.Add
Set wordDoc = WordApp.activedocument
With .Selection
.Font.Size = 14
.Font.Bold = True
.paragraphformat.Alignment = 1
.typetext Text:="MY REPORT"
.typeparagraph
.typeparagraph
.Font.Size = 12
.paragraphformat.Alignment = 0
.Font.Bold = False
.typetext Text:="Date:" & vbTab & _
Format(Date, "mmmm d, yyyy")


I would have expected this example of "very early" binding to execute significantly quicker, but I was disappointed. Any ideas as to why. Is my binding strategy flawed in some way? I have set the reference to Word.

Bob Phillips
12-21-2005, 07:42 AM
There is not enough travel back and forth between Excel and the Word type library to make the early binding noticeable.

Killian
12-21-2005, 08:18 AM
I don't see any flaw with what you've done.
With late-binding, at run-time, the class object must be found and each subsequent reference compiled whereas with early-binding that work is already done.
I'm not sure what kind of performance increase you were expecting but it is reasonable to expect early-binding to be much more efficient.

I ran a couple of quick comparison tests (using your code looped a number of times) and found late-binding took around 2-3 times as long as early-binding.
However, whether this is noticable in a single instance on a fast PC is debatable.