以下宏很好用。它打开了IE的实例并使用“getelementsbyclassname”方法返回了“my_rate”的预期值。然而,当我运行第二个宏使用“MSXML2”方法时,代码在注明的那一行失败,并出现“Run-time error 438: Object doesn't support this property or method”的错误。为什么“Open IE”方法可以工作,但“MSXML2”方法会使我的代码失败?我正在使用IE 11。我也为第二个宏设置了对Microsoft HTML对象库的引用,但似乎没有任何区别。谢谢你事先解释给我。
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
' 运行时错误 438:对象不支持此属性或方法出现在上面的行
结束子程序
编辑:D. Zemens 的库截图
Dim doc As HTMLDocument
,然后在代码中稍后使用Set doc = New HTMLDocument: doc.body.innerHTML = xml_obj.responseText
。然后Set el = doc.getelementsbyclassname("br-col-2 br-apr")
就可以正常工作了吗? - user688334