Windows 10 Internet Explorer不适用于旧的VBA代码

时间:2019-01-16 20:05:53

标签: excel vba internet-explorer windows-10

几年前,我在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?

1 个答案:

答案 0 :(得分: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

输出:

enter image description here

如果我们谈论您上面发布的代码,则可以尝试将断点放在该行上并尝试调试代码。检查单元格B5包含哪个值,并检查该行是否成功执行。为了进行测试,请尝试为该文本框分配静态值。

相关问题