vba - Internet Explorer VBA 自动化错误 : The object Invoked has disconnected from its clients

标签 vba excel-2010 internet-explorer-11

我正在尝试编写从 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

最佳答案

更改为:

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

解决了问题。另外,我确实需要重新添加评论中提到的 Do 循环:
Do
    DoEvents
Loop Until ie.ReadyState = READYSTATE_COMPLETE

关于vba - Internet Explorer VBA 自动化错误 : The object Invoked has disconnected from its clients,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42403210/

相关文章:

vba - 在 VBA 中调试 "Search for Name"代码

vba - 减少 If 语句 VBA

javascript - 如何在 IE11 中使用 css 垂直拉伸(stretch) span 到其父元素 <td>

vba - 对于循环设置字体和范围内部花费的时间太长

excel - 动态替换vba中的文本

vba - 自动选择删除工作表而不弹出消息

vba - Excel:使用 VBA 获取单元格的背景颜色

Excel-比较不同工作表中的两个单元格,如果为真则从其他单元格复制值

internet-explorer - Internet Explorer 11 不会在网站上设置 cookie

Angular 元素输出在 IE11 中不起作用