我正在尝试使用 VBA 从 Excel 访问网页。我可以启动 Internet Explorer,并且看到网页出现,但当我点击 Do Until internet.ReadyState >= 4 行代码时,出现运行时错误 462。有任何想法吗?最终我希望能够解析一个网站并获取该网站上的链接列表并选择一个,然后“单击”该链接。建议和帮助会很好。这是我正在使用的函数(我在网上找到的):
Public Sub clicklick()
Dim internet As Object
Dim internetdata As Object
Dim div_result As Object
Dim header_links As Object
Dim link As Object
Dim URL As String
Set internet = CreateObject("InternetExplorer.Application")
internet.Visible = True
URL = "https://www.google.co.in/search?q=how+to+program+in+vba"
internet.Navigate URL
Do Until internet.ReadyState >= 4
DoEvents
Loop
Application.Wait Now + TimeSerial(0, 0, 5)
Set internetdata = internet.Document
Set div_result = internetdata.getelementbyid("res")
Set header_links = div_result.getelementsbytagname("h3")
For Each h In header_links
Set link = h.ChildNodes.Item(0)
Cells(Range("A" & Rows.count).End(xlUp).row + 1, 1) = link.href
Next
MsgBox "done"
End Sub
Run Code Online (Sandbox Code Playgroud)
谢谢你,艾伦