The following macro works just fine. It opens an instance of IE and uses the "getelementsbyclassname" method to return the expected value for "my_rate". However when I run the second macro which uses the "MSXML2" method, the macro fails on the noted line and a "Run-time error 438: Object doesn't support this property or method" error occurs. Why does the "Open IE" method work, but the "MSXML2" method fail with my code? I am running with IE 11. I also have a reference set to the Microsoft HTML Object Library for the second macro, but it doesn't seem to make a difference. Thanks in advance for explaining this to me.
Sub BankRate_Rate_Retrieval()
my_url = "http://www.bankrate.com/funnel/mortgages/mortgage-results.aspx?market=321&loan=150000&perc=20&prods=2&points=0"
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = True
.Navigate my_url
.Top = 50
.Left = 530
.Height = 400
.Width = 400
End With
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
my_rate = ie.Document.getelementsbyclassname("br-col-2 br-apr")(1).getElementsByTagName("div")(0).innertext
End Sub
Sub BankRate_Rate_Retrieval()
my_url = "http://www.bankrate.com/funnel/mortgages/mortgage-results.aspx?market=321&loan=150000&perc=20&prods=2&points=0"
Set html_doc = CreateObject("htmlfile")
Set xml_obj = CreateObject("MSXML2.XMLHTTP")
xml_obj.Open "GET", my_url, False
xml_obj.send
html_doc.body.innerhtml = xml_obj.responseText
Set xml_obj = Nothing
my_rate = html_doc.body.getelementsbyclassname("br-col-2 br-apr")(1).getElementsByTagName("div")(0).innertext
' Run-time error 438: Object doesn't support this property or method occurs on above line
End Sub
Edit: Library Screenshot for D. Zemens
The error message is pretty straightforward:
GetElementsByClassName
is not a method available in the Microsoft XML, v6.0 library.
You can review the available methods, here:
http://msdn.microsoft.com/en-us/library/aa926433.aspx
And while I can't find a similar documentation link, if you enable referenc to the MSHTML library, you can review there to confirm likewise, there is not GetElementsByClassName
method. This is a method that is available to IE automation but not to HTML or DOMDocument.
UPDATED
While this may not resolve your problem, I put it here in case it helps others with IE8. It seems to be working for this purpose, but may need to be refined.
Option Explicit
Sub BankRate_Rate_Retrieval()
Dim my_url As String
Dim html_doc As Object 'HTMLDocument
Dim xml_obj As Object 'MSXML2.DOMDocument
Dim my_rate As String
my_url = "http://www.bankrate.com/funnel/mortgages/mortgage-results.aspx?market=321&loan=150000&perc=20&prods=2&points=0"
Set html_doc = CreateObject("htmlfile")
Set xml_obj = CreateObject("MSXML2.XMLHTTP")
xml_obj.Open "GET", my_url, False
xml_obj.send
html_doc.body.innerhtml = xml_obj.responseText
'attempt to replicate the GetElementsByClassName for IE8
my_rate = IE8_GetElementsByClassName(html_doc.body, "br-col-2 br-apr", 1).GetElementsByTagName("div")(0).InnerText
MsgBox my_rate
Set xml_obj = Nothing
Set html_doc = Nothing
End Sub
Function IE8_GetElementsByClassName(html As Object, className As String, Optional Position As Integer)
'Function to return an array of matching classname elements
' or if specified will return a single HTMLElement by Position index
Dim eleDict As Object
Dim ele as Variant
Set eleDict = CreateObject("Scripting.Dictionary")
For x = 0 To html.all.Length - 1
Set ele = html.all(x)
If ele.className = className Then
'Debug.Print i & vbTab & x & vbTab & ele.InnerText
Set eleDict(i) = ele
i = i + 1
End If
Next
If Position = Empty Then
IE8_GetElementsByClassName = eleDict.Items
Else
Set IE8_GetElementsByClassName = eleDict(Position)
End If
Set eleDict = Nothing
End Function