2017-02-05 69 views
0

我想使用VBA從excel訪問網頁。我可以啓動Internet Explorer,並且看到網頁出現,但是當我點擊「直到internet.ReadyState> = 4行代碼」時,出現運行時錯誤462。有任何想法嗎?最終我希望能夠解析網站並獲取該網站上的鏈接列表並選擇一個,然後在該鏈接上單擊「單擊」。建議和幫助會很好。這裏是函數(即我在網上找到),我現在用:與Internet Explorer的運行時錯誤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 

謝謝你, 艾倫

+0

該代碼似乎沒關係,並獲得鏈接。什麼錯誤來到你的電腦。把錯誤截圖也是如此。 – harun24hr

回答

0

嘗試replcaing臺詞:

Do Until internet.ReadyState >= 4 
    DoEvents 
Loop 

Application.Wait Now + TimeSerial(0, 0, 5) 

有了:

While internet.busy 
    DoEvents 
Wend 

完整代碼(測試)

Option Explicit 

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 h 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 

internet.Visible = True 

While internet.busy 
    DoEvents 
Wend 

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 
+0

Shai,謝謝你的幫助。這工作。我現在的問題是,我想真正使用這個網站(https://biz.yahoo.com/p/s_conameu.html),我想獲得該網站上列出的各個部門的鏈接。當我運行代碼時,我得到了一切:設置div_result = internetdata.getelementbyid(「res」),我得到一個運行時錯誤'424'對象所需的消息。使用這種類型的網站嘗試相同的事情是否存在問題? – user3610196