【问题标题】:Getting IE Automation Error "Object variable or with block variable not set"获取 IE 自动化错误“对象变量或未设置块变量”
【发布时间】:2021-04-15 16:28:42
【问题描述】:

我在自动化现有 IE 窗口时遇到错误。 错误是

“对象变量或未设置块变量” 代码是

Sub accessExistingIEBrowser()

      boolWindowFound = False
      Set objShell = CreateObject("Shell.Application")
      WinCount = objShell.Windows.Count
      
       For winNo = 0 To (WinCount - 1)

          strURL = objShell.Windows(winNo).document.Location
          strTitle = objShell.Windows(winNo).document.Title
          If strTitle Like "Sample Form" Then
              Set IE = objShell.Windows(winNo)
              boolWindowFound = True
              Exit For
          Else
          End If

      Next

      If boolWindowFound Then
        Set doc = IE.document
        doc.getElementsByName("fname")(0).Value = "test"
      End If

End Sub

【问题讨论】:

  • 您假设每个窗口都有一个非空的document。事实并非如此。

标签: excel vba automation excel-automation ie-automation


【解决方案1】:

“未设置对象变量或块变量”表示您在代码中的某处省略了 Set 语句。在这段代码的开头设置一个断点(F9),开始调试(F5)和单步(F8)通过你的代码,你会发现在哪里。

【讨论】:

  • 为什么投反对票?断点和单步执行是成功调试代码的首要条件。
猜你喜欢
  • 1970-01-01
  • 2014-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多