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