下面的代码在第一次运行时有效,但在随后的运行中经常失败。它失败的行在下面注释。我认为循环通过 SHDocVw.ShellWindows 会产生问题,我需要在再次运行之前清理一些东西。要复制问题,请在 IE 中打开 google,然后运行此过程,重复。谢谢你的帮助。确切的错误是 Exception from HRESULT: 0x800A01B6 。(请注意,这是更复杂代码的简化版本,如果已设置,我实际上会多次重用 IE_test。)
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