Compiling and Late Binding

T

Tim Childs

Hi

in the few lines of code below which was from Dave Petersen and Joe U, the
use of the Excel command ForceFullCalculation does not produce a standard
compilation error (due to late binding apparently), whereas it would if it
was just
Thisworkbook.ForceFullCalculation

If Val(Application.Version) > 11 Then
Set oWkBk = ThisWorkbook
oWkBk.ForceFullCalculation = True
Debug.Print "Running forced calculation"
End If

Can someone explain briefly WHY late binding applies in this area?

Many thanks

Tim
 
D

Dave Peterson

Just my thoughts -- could be completely wrong technically. But it satisfies my
curiosity <vbg>.

Excel knows all the properties and methods for ThisWorkbook just like it knows
all the properties and methods for a Range object.

But it has no idea what you're doing with a variable declared as an Object.

So no matter what version you're using if you use ThisWorkbook, excel knows --
you can't fool it.

But if you use the Object variable, excel doesn't know or care until the code
actually runs.
 
M

Martin Brown

Hi

in the few lines of code below which was from Dave Petersen and Joe U,
the use of the Excel command ForceFullCalculation does not produce a
standard compilation error (due to late binding apparently), whereas it
would if it was just
Thisworkbook.ForceFullCalculation

If Val(Application.Version) > 11 Then
Set oWkBk = ThisWorkbook
oWkBk.ForceFullCalculation = True
Debug.Print "Running forced calculation"
End If

Can someone explain briefly WHY late binding applies in this area?

Excel doesn't bother to check if the object oWkBk can execute the
statement "ForceFullCalculation = True" until it reaches that line.

Protected with this code you can insert completely invalid invocations
of non-existent and fictitious methods of oWkBk eg.

oWkBk.Zork Xyzzy = "Hello Sailor"

It will give a runtime error 438 "Object doesn't support property of
method" if you ever execute the line but not until. So it is quite a
clever way to hide Version related incompatibilities from the compiler.

I am pretty sure no versions of XL ever supported the Zork method.
(a prehistoric text based adventure game)

Regards,
Martin Brown
 
T

Tim Childs

Hi Martin and Dave

MANY thanks for those responses - interesting to hear what the experts get
up to with overcoming version incompatibilities.

best wishes

Tim

("Qu: Upgrades - Progress isn't all what it's cracked up to be?").
 
Top