【问题标题】:Windows 10 Internet Explorer is not working with old VBA codeWindows 10 Internet Explorer 无法使用旧 VBA 代码
【发布时间】:2019-01-16 20:05:53
【问题描述】:

几年前,我在 VBA 中编写了代码,在 IE 中打开一个网站,并用 Excel 文件中的数据填充文本框。不幸的是,我现在使用的是 Windows 10,这个程序不再工作了。它可以毫无问题地打开网站,但无法将数据转置到文本框中。它只是打开网站并停止(不输入数据)。

该程序仍然可以在 Windows 7 的 IE 中运行,没有任何问题。我尝试了多台装有 Windows 10 的笔记本电脑,但问题再次出现。

我真的不知道如何解决这个问题。

DoEvents
WebAddress = "CONFIDENTIAL URL HERE" & WebID & "&t=" 

        Dim IE As Object

    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual


  Set IE = CreateObject("InternetExplorer.Application")

  IE.Navigate WebAddress
  IE.Visible = True
  While IE.busy
    DoEvents  'wait until IE is done loading page.
  Wend

  Application.Wait (Now + TimeSerial(0, 0, 4))

  IE.Document.All("Response_123").Value = ThisWorkbook.Sheets("Sheet1").Range("B5")

最后一行代码应该将数据从 excel 文件传输到 Internet Explorer 中的文本框,但是没有任何反应,文本框保持空白。我是否需要更改代码中的任何内容以适应 Windows 10 IE?

【问题讨论】:

  • Response_123 是否在任何地方定义/分配?
  • 如果您无法共享 URL,您至少应该共享您尝试与之交互的 HTML。此外,您应该展示Response_123 是如何定义为@MathieuGuindon 建议的。
  • 另外,为什么是最初的 DoEvents?并使用 .Navigate2 并在该行之后使用 While IE.Busy Or IE.readyState
  • 因缺少合适的minimal reproducible example而投票关闭

标签: excel vba internet-explorer windows-10


【解决方案1】:

您可以看到您的代码没有以有效的方式编写。对于另一种方法,您可以参考下面的示例,该示例适用于 Windows 10。

Dim IE As Object

Sub demo()
    Application.ScreenUpdating = False

    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True
    IE.Navigate "C:\Users\Administrator\Desktop\sample.html"


    While IE.Busy
        DoEvents
    Wend

    wait 1
    IE.Document.getElementById("firstname").Value = ThisWorkbook.Sheets("Sheet1").Range("A1")
    wait 1
    IE.Document.getElementById("lastname").Value = ThisWorkbook.Sheets("Sheet1").Range("A2")
    wait 1
    IE.Document.getElementById("submit_btn").Click

    IE.Quit
    Set IE = Nothing

    Application.ScreenUpdating = True
End Sub

Private Sub wait(seconds As Long)
    Dim endTime As Date
    endTime = DateAdd("s", seconds, Now())
    Do While Now() < endTime
        DoEvents
    Loop

End Sub

输出:

如果我们谈论您上面发布的代码,那么您可以尝试在该行放置断点并尝试调试代码。检查 Cell B5 包含的哪个值,并检查该行是否成功执行。出于测试目的,尝试为该文本框分配静态值。

【讨论】:

  • Deepak.. 我将我的代码更改为您所拥有的,但不是“名字”,而是使用当我右键单击 > 检查文本框时显示的 ID。不幸的是,它仍然没有填写文本框......它们仍然是空白的。
猜你喜欢
  • 1970-01-01
  • 2012-12-10
  • 2013-03-30
  • 1970-01-01
  • 2012-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多