【问题标题】:Internet Explorer VBA Automation Error: The object Invoked has disconnected from its clientsInternet Explorer VBA 自动化错误:被调用的对象已与其客户端断开连接
【发布时间】:2017-07-13 04:39:39
【问题描述】:

我正在尝试编写从 Excel 读取值的代码,在基于 Web 的内部系统中查找它并将结果存储回 Excel。它读取 Excel 没有问题,打开 Internet Explorer 也没有问题,但是当我尝试引用已打开的内容时,出现上述错误。 “ie.Navigate url”行有效,但下一行“Set DOC = ie.Document”会产生错误。关于造成这种情况的任何想法?这是我的代码:

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

【问题讨论】:

  • 看起来您可能需要一个就绪/等待循环? (在此处搜索该术语,我相信您会找到实现该术语的示例)这个想法是 navigate 不会立即发生,并且在页面加载时,浏览器可能不会可用于自动化请求等。
  • 不在上面的示例代码中,但我在代码中确实有以下内容:“DoDoEventsLoop until ie.ReadyState = READYSTATE_COMPLETE”它给了我同样的错误循环直到行。
  • 如果我运行调试并让它静置(我可以看到它在 IE 上显示,我仍然会收到错误)。

标签: vba excel-2010 internet-explorer-11


【解决方案1】:

改为:

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

解决了这个问题。另外,我确实需要添加回 cmets 中提到的 Do 循环:

Do
    DoEvents
Loop Until ie.ReadyState = READYSTATE_COMPLETE

【讨论】:

  • 为我工作。惊人的。谢谢。只是想知道你是怎么想出来的?请告诉我:)
  • 我一直在网上寻找答案。我不记得我是在这里还是在其他地方找到的,但我看到其他人使用上述代码完成了他们的代码,所以我尝试了一下,它成功了。
猜你喜欢
  • 2013-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-31
相关资源
最近更新 更多