Excel VBA: difference between declaring a shared object and specifying an object type

I found unexpected (at least for me) behavior using a variable declared as Objectin Excel-vba. I tried to extract some data from the html page, so I used the following procedure:

sub Test

Dim htmlPage As htmlDocument
'This is the interesting variable
Dim DataTable       As Object
Dim DataCollection  As Object

'Code to get the html page

Set DataTable = htmlPage.getElementById("a name")
Set DataCollection = DataTable.getElementsByClassName("another name")

' Code to use DataCollection

end Sub

If I try to run this code in a line Set DataCollection = DataTable.getElementsByClassName("another name"), I get an error

Runtime Error '438'

The object does not support this property or method.

After the first command, the Setvariable DataTablebegan to have a type Object/HTMLDivElement, therefore, in principle, it should have a method getElementsByClassName.

I do not understand that if I declare a variable from the beginning as HTMLDivElement(without changing anything in the braid), for example:

Dim DataTable       As HTMLDivElement

. DataTable HTMLDivElement/HTMLDivElement.

Object vs ? ?

+1

Source: https://habr.com/ru/post/1620237/


All Articles