2012-08-02 17 views
0

下面的代碼在第一次運行時工作,但在後續運行中經常失敗。 下面評論它失敗的行。我認爲循環SHDocVw.ShellWindows會產生這個問題,我需要清理一些東西,然後再運行它。要複製該問題,請在IE中打開Goog​​le,然後運行此過程,重複此操作。謝謝你的幫助。確切的錯誤是來自HRESULT的異常:0x800A01B6。 (請注意,這是一個比較複雜的代碼,我如果它被設置其實也重用IE_test多次的簡化版本。)有時無法獲取.Document.Body.InnerHtml

Public IE_test As SHDocVw.InternetExplorer 
    Sub TestIE() 
    Dim shellWindows_3 As New SHDocVw.ShellWindows() 
    Dim htmlDoc As String 
    Dim link1 = "google.com" 
    If IsNothing(IE_test) = True Then 
     For Each ie_x As SHDocVw.InternetExplorer In shellWindows_3 
      If ie_x.LocationURL.Contains(link1) Then 'find the google instance 
       IE_test = ie_x 
      End If 
     Next 
    End If 

    With IE_test 
     .Visible = True 
     htmlDoc = .Document.Body.InnerHtml 'Fails here on second run 
     .Quit() 
    End With 

    IE_test = Nothing 
    MsgBox(Len(htmlDoc)) 

End Sub 

回答

1

我最終被完全從任何程序刪除.Quit()解決這一問題(直到該方案關閉)。現在,我總是重用IE的現有實例,而不是關閉並重新打開,這樣可以避免錯誤。