2

I'm under the impression that an object MUST support all methods/properties upon being initialized or else the program will create an error. However, if you see below I am defining 2 variables (doc and el) as htmldocuments. Thus any object assigned to these variables must in theory support all htmldocument methods/properties, otherwise an error will be raised. In this scenario doc is the control variable and el is the test variable. "doc" is set to a document object, which can obviously support all htmldocument interface methods. "el" is set to a single htmlparagraphelement, which CANNOT support all htmldocument interface methods. However, no error occurs. In the next line, I test the htmldocument interface method called "createRange" on both variables. It works fine (as expected) with the doc variable. An error is raised with the el variable saying "Object doesn't support this method". This is also expected, but what I don't understand how I got this far in the first place. Shouldn't an error have been raised when the object was initialized?

VBA:

'Turn on references to Microsoft Internet Controls
'and Microsoft HTML Object Libraries before running

Sub qstn()

Dim IE As New InternetExplorer
'Note that both doc and el are declared within the HTMLdocument interface
Dim doc As HTMLDocument, el As HTMLDocument

IE.navigate "https://www.youtube.com/"

IE.Visible = True

Do Until IE.ReadyState = 4: Loop

'No error expected here.  Document object is returned
'which has all properties and methods of HTMLDocument interface
Set doc = IE.Document

'Why am I not getting a type mismatch here?
'I should not be allowed to go any further on this line
Set el = IE.Document.getElementsByTagName("p")(0)

'Random method to prove that the actual document object can execute this method
doc.createRange

'Same method used on the object that was also declared as HTMLdocument,
'but was set as an htmlparagraphelement.  This will not working
'because the htmlparagraphelement doesn't support it
el.createRange

End Sub
apdm
  • 121
  • 2

1 Answers1

2

For the most part, VBA uses late binding, not the kind of early binding that you are describing.

In strongly-typed languages like C#, you will get the behavior you expect, because the compiler will check your method calls against the class definitions and complain that they don't match. This is early-binding.

But in a loosely-typed, late-binding language like VBA, you don't get the error until the interpreter actually attempts to execute the offending code.

Robert Harvey
  • 198,589
  • 55
  • 464
  • 673
  • What about this: I declare FSO as a FileSystemObject (with references to Microsoft Scripting Runtime turned on) Then I do Set FSO = New Dictionary (which is an object in the same library). This produces a type mismatch error immediately. Any thoughts on why? – apdm Jul 13 '15 at 21:19
  • `FileSystemObject` is a COM object. VBA can check the dispatch table in a COM object, and identify bad method calls. Attempting to call a nonexistent method on a COM object might actually cause a crash. Note that VBA is not an object-oriented language, but merely an object-based one. It lacks many facilities that full-blown object oriented languages provide. – Robert Harvey Jul 13 '15 at 21:24
  • If FileSystemObject is a COM object, what is an HTML document object? – apdm Jul 13 '15 at 21:43
  • It's also a COM object. However, see [Using early binding and late binding in Automation](https://support.microsoft.com/en-us/kb/245115) and [The Document Property of the InternetExplorer Object](https://msdn.microsoft.com/en-us/library/aa752052(v=vs.85).aspx). In particular (in the second link): *"Microsoft Visual Basic programs that early bind to the HTMLDocument object can reference it through the IHTMLDocument interface, the IHTMLDocument2 interface, and the IHTMLDocument3 interface."* – Robert Harvey Jul 13 '15 at 22:19