【问题标题】:Excel VBA Code Not Working With Internet Explorer 11Excel VBA 代码不适用于 Internet Explorer 11
【发布时间】:2019-08-08 00:25:13
【问题描述】:

我有一个 vba 代码,可以将数据从 Excel 表上传到网站。但是,该代码在 IE 浏览器 8 中运行良好,但在 win8 IE 浏览器 11 上无法运行。它给出错误 438(“对象不支持此属性或方法”)并且文本字段保持空白。以下是部分代码:

Dim ie As New InternetExplorer

将 DOCS 变暗为 HTMLDocument

ie.Navigate "http://uhs.edu.pk/results/etr2015.php"

ie.Visible = True

做

DoEvents

循环直到 ie.readyState = READYSTATE_COMPLETE

ie.Document.getElementsName("ROLLNO").Value = "55"

需要帮助来解决这个问题。

【问题讨论】:

  • 出现错误时,按调试,输入即时窗口? TypeName(ie),然后发布输出。也检查this answer。
  • 在即时窗口中输入“?TypeName(ie)”后,我发现以下消息:IWebBrowser2
  • 非常感谢您的宝贵回复。我还尝试了以下一个,它完全符合我的要求。即.Document.forms("result").elements("rollno").Value = "12"

标签: html excel internet-explorer-11 vba


【解决方案1】:

这应该适合你。

Sub testIE()
Dim ie As Object
Dim objCollection As Object
Dim i As Integer

'Create InternetExplorer Object
Set ie = CreateObject("InternetExplorer.Application")

    ie.Visible = True
    'Load the login page
    ie.navigate "http://uhs.edu.pk/results/etr2015.php"

    'Wait until the page is ready
    Do While ie.busy
        Application.Wait DateAdd("s", 1, Now)
    Loop

    'Get all the elements with input tag name
    Set objCollection = ie.Document.getElementsByTagName("input")

    i = 0

    'Loop through all elements and find login form and fill it
    While i < objCollection.Length
        'Login name
        If objCollection(i).Name = "rollno" Then
            objCollection(i).Value = "55"
        End If  
    i = i + 1
    Wend



    'Clean up
    Set ie = Nothing


End Sub

如果您还想自动化搜索按钮,这应该可以解决问题

Sub testIE()
Dim ie As Object
Dim objCollection As Object
Dim objElement As Object
Dim i As Integer

'Create InternetExplorer Object
Set ie = CreateObject("InternetExplorer.Application")

    ie.Visible = True
    'Load the login page
    ie.navigate "http://uhs.edu.pk/results/etr2015.php"

    'Wait until the page is ready
    Do While ie.busy
        Application.Wait DateAdd("s", 1, Now)
    Loop

    'Get all the elements with input tag name
    Set objCollection = ie.Document.getElementsByTagName("input")

    i = 0

    'Loop through all elements and find login form and fill it
    While i < objCollection.Length
        'Login name
        If objCollection(i).Name = "rollno" Then
            objCollection(i).Value = "55"
        End If

        'Store login button in object
        If objCollection(i).Type = "submit" Then
            Set objElement = objCollection(i)
        End If

    i = i + 1
    Wend

    'Click login
    objElement.Click


    'Clean up
    Set ie = Nothing


End Sub

从新页面获取数据的代码

Sub testIE()
Dim ie As Object
Dim objCollection As Object
Dim objElement As Object
Dim i As Integer

'Create InternetExplorer Object
Set ie = CreateObject("InternetExplorer.Application")

    ie.Visible = False
    'Load the login page
    ie.navigate "http://uhs.edu.pk/results/etr2015.php"

    'Wait until the page is ready
    Do While ie.busy
        Application.Wait DateAdd("s", 1, Now)
    Loop

    'Get all the elements with input tag name
    Set objCollection = ie.Document.getElementsByTagName("input")

    i = 0

    'Loop through all elements and find login form and fill it
    While i < objCollection.Length
        'Login name
        If objCollection(i).Name = "rollno" Then
            objCollection(i).Value = "55"
        End If

        'Store login button in object
        If objCollection(i).Type = "submit" Then
            Set objElement = objCollection(i)
        End If

    i = i + 1
    Wend

    'Click login
    objElement.Click

    Do While ie.busy
        Application.Wait DateAdd("s", 1, Now)
    Loop

    Set objCollection = ie.Document.getElementsByTagName("tr")


    Range("A2").Value = Split(objCollection(23).innertext, "Sr. No.")(1)

    Range("B2").Value = Split(objCollection(24).innertext, "Roll No.")(1)

    Range("C2").Value = Split(objCollection(25).innertext, "Name of the Candidate")(1)

    Range("D2").Value = Split(objCollection(26).innertext, "Father's Name ")(1)

    Range("E2").Value = Split(objCollection(27).innertext, "Centre")(1)

    Range("F2").Value = Split(objCollection(28).innertext, "Entrance Test Marks ")(1)

    Range("G2").Value = Split(objCollection(29).innertext, "Total Marks")(1)


    'Clean up
    ie.Quit
    Set ie = Nothing


End Sub

希望对你有所帮助:)

其他页面上组合框的解决方案。

Sub testIE()
Dim ie As Object
Dim objCollection As Object
Dim objElement As Object
Dim i As Integer

'Create InternetExplorer Object
Set ie = CreateObject("InternetExplorer.Application")

    ie.Visible = True
    'Load the login page
    ie.navigate "http://result.biselahore.com/"

    'Wait until the page is ready
    Do While ie.busy
        Application.Wait DateAdd("s", 1, Now)
    Loop

    'Get all the elements with input tag name
    Set objCollection = ie.Document.getElementsByTagName("Select")

    i = 0

    'Loop through all elements and find login form and fill it
    While i < objCollection.Length

        'either one of the methodes below works
        'If objCollection(i).Name = "session" Then objCollection(i).Value = 2
        If objCollection(i).Name = "session" Then objCollection(i).Item(2).Selected = True

        If objCollection(i).Name = "year" Then objCollection(i).Item(2).Selected = True
        i = i + 1
    Wend

    'Clean up
    Set ie = Nothing


End Sub

【讨论】:

  • 还有一个问题,它在移动到下一页后没有下载所需的数据,但单元格留空。下面提到了我用来用页面数据填充 excel 单元格的代码。 ie.Document.getElementsByTagName("td")(7).innerText ie.Document.getElementsByTagName("td")(9).innerText ie.Document.getElementsByTagName("td")(11).innerText 请帮忙解决这个问题问题。
  • 你从来没有指定你想要什么数据以及你想要数据在哪个单元格中?
  • "uhs.edu.pk/results/etr2015.php";是我输入学生卷号的页面,如卷#12。它将结果带到下一页,我需要在 excel 单元格中使用相同的数据,如 a2、b2、c2、d2....... 提到了结果前进::::::::::::Sr。编号:12.. 第 12 号卷... 候选人姓名:ALISHA FATIMA.... 父亲姓名:SYED TARIQ BUKHARI.... 入学考试分数:916 等...
  • 在我的回答中给出最后一个代码:) 应该为你解决问题
  • 不幸的是,没有多少关于 VB 或 VBA 的指南。 Alot 认为这是一种过时的编程语言,但我相当肯定它会持续多年。我远非 VBA 专家,但我尝试尽可能多地使用它来了解更多信息。 IE 自动化是我迄今为止所做的最多的事情。我最好的建议是边做边学……堆栈溢出是寻找灵感的好地方,但不要复制所有代码。那么你永远不会明白它是如何真正起作用的。搜索灵感并构建自己的代码。
猜你喜欢
  • 2013-07-25
  • 2013-07-05
  • 2014-08-24
  • 2013-12-19
  • 2014-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-18
相关资源
最近更新 更多