PDA

View Full Version : VBA Compiler broken?



Frosty
07-26-2019, 10:07 AM
I've tested this in Office 365 and 2016... is this is some kind of fundamental change to the compiler, or have I just missed this bug all these years?


Sub Test1
Dim p As Paragraph


Set p = Selection.Paragraphs.First

Test2 p.Range
End Sub

Sub Test2(p as Paragraph)

Debug.Print p.Range.Text
End Sub


Why does that only provoke a run-time error of type mismatch? Shouldn't it provoke a compile error?

gmaxey
07-26-2019, 11:08 AM
Jason,

Tested your code in Word 2013, 2010, 2007 and 2003 with same result. I suppose it has just been missed.

Frosty
07-26-2019, 12:36 PM
Thanks, Greg... so I'm just... crazy. I can't believe I've never noticed that. I rely on the compiler to catch my mistakes all the time.

It catches if I try to pass a footnote object when the routine is expecting a document object... seems strange to mess up on this.

Paul_Hossler
07-29-2019, 06:09 AM
A guess -- Word seems to have a complex object model with (IMO) some 'overlap' in things like Selection, Range, etc. returnng even more 'lower level' objects

P and P.range are objects and pass the the 'compile time' checks since Test2 is expecting an object address, and it doesn't fail until R/T

A non-object variable is caught at compile time

As I said, this is a guess and as we all know "Mysterious Are the Ways of MS Word"


24700