2017-02-22 89 views
4

我試圖編寫代碼,它將從Excel讀取值,在基於內部Web的系統中查找它並將結果存儲回Excel中。它讀取沒有問題的Excel,沒有問題打開Internet Explorer,但是當我嘗試引用已打開的內容時,出現上述錯誤。 「ie.Navigate url」這一行起作用,但下一行「Set DOC = ie.Document」會產生錯誤。任何想法是什麼導致這個?這裏是我的代碼:Internet Explorer VBA自動化錯誤:調用的對象已與客戶端斷開連接

Public Sub getClient() 
    Dim xOpen As Boolean 
    xOpen = False 
    Dim row As Long 

    Dim xL As Excel.Application 
    Set xL = New Excel.Application 
    xL.Visible = False 
    Dim wb As Excel.Workbook 
    Dim sh As Excel.Worksheet 

    'Change the name as needed, out put in some facility to input it or 
    'process multiples... 
    Dim filename As String 
    filename = "auditLookup.xlsx" 
    Set wb = xL.Workbooks.Open(getPath("Audit") + filename) 
    xOpen = True 
    Set sh = wb.Sheets(1) 

    Dim ie As Variant 
    Set ie = CreateObject("InternetExplorer.Application") 
    ie.Visible = True 

    Dim DOC As HTMLDocument 
    Dim idx As Integer 
    Dim data As String 

    Dim links As Variant 
    Dim lnk As Variant 
    Dim iRow As Long 
    iRow = 2   'Assume headers 

    Dim clientName As String 
    Dim clientID As String 
    Dim nameFound As Boolean 
    Dim idFound As Boolean 
    Dim url As String 

    While sh.Cells(iRow, 1) <> "" 
    'Just in case these IDs are ever prefixed with zeroes, I'm inserting 
    'some random character in front, but removing it of course when 
    'processing. 
    url = "https://.../" + mid(sh.Cells(iRow, 1), 2) 
    ie.navigate url 
    Set DOC = ie.Document 

    'Search td until we find "Name:" then the next td will be the name. 
    'Then search for "P1 ID (ACES):" and the next td with be that. 
    Set links = DOC.getElementsByTagName("td") 
    clientName = "" 
    clientID = "" 
    nameFound = False 
    idFound = False 
    For Each lnk In links 
     data = lnk.innerText 
     If nameFound Then 
      clientName = data 
     ElseIf idFound Then 
      clientID = data 
     End If 
     If nameFound And idFound Then 
      Exit For 
     End If 

     If data = "Name:" Then 
      nameFound = True 
     ElseIf data = "P1 ID (ACES):" Then 
      idFound = True 
     End If 
    Next 
    sh.Cells(iRow, 2) = clientName 
    sh.Cells(iRow, 2) = clientID 
    iRow = iRow + 1 
    Wend 

    Set ie = Nothing 
    If xOpen Then 
    wb.Save 
    Set wb = Nothing 
    xL.Quit 
    Set xL = Nothing 
    Set sh = Nothing 
    xOpen = False 
    End If 
Exit Sub 
+3

看起來也許你需要一個就緒/等待循環? (在這裏搜索這個術語所以我相信你會發現實現這個術語的exmaples)這個想法是* navigate *不會立即發生,當頁面加載時,瀏覽器可能無法用於自動化請求, etc. –

+0

不在上面的示例代碼中,但我確實在代碼中包含以下內容:「Do DoEvents Loop Until ie.ReadyState = READYSTATE_COMPLETE」,它給出了Loop Until行中的相同錯誤。 – PKatona

+0

如果我運行調試,並讓它坐下來(我可以看到它在IE上出現,我仍然得到錯誤)。 – PKatona

回答

3

更改爲:

Dim ie As InternetExplorer 
Set ie = New InternetExplorerMedium 
... 

解決的問題。另外我確實需要添加評論中提到的Do循環:

Do 
    DoEvents 
Loop Until ie.ReadyState = READYSTATE_COMPLETE 
+0

爲我工作。真棒。謝謝。只是好奇想知道你是怎麼想出來的?請讓我知道:) –

+0

我一直在網上尋找答案。我不記得我是否在這裏或其他地方找到它,但我看到有人使用上面的代碼完成了他們的代碼,所以我嘗試了它並運行。 – PKatona

相關問題