【问题标题】:Unable to reset HTML document after navigating to page in VBA导航到 VBA 中的页面后无法重置 HTML 文档
【发布时间】:2016-11-04 05:11:18
【问题描述】:

我正在使用 VBA 导航 IE,但在重置文档对象以便可以浏览多个页面时遇到问题。我使用this 问题的答案来帮助创建原始脚本,但我遇到了一个与海报非常相似的问题。不幸的是,列出的答案并不能帮助或指导我找到解决方案。

我正在使用以下代码来拉取页面并进行导航。

Sub UpdatesalesNotes()

Dim ie As Object, ieDoc As HTMLDocument

Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True

strHTML = ActiveSheet.Range("A31").Value

ie.navigate strHTML

'wait for browser
While ie.ReadyState <> READYSTATE_COMPLETE
    DoEvents
Wend


Set ieDoc = ie.Document

Call ClickButton(ieDoc, "New Service Note")

'wait for browser
While ie.ReadyState <> READYSTATE_COMPLETE
    DoEvents
Wend


ieDoc.getElementById("00Nj0000009FpF9").Value = "Placeholder Text"


Call ClickButton(ieDoc, "Save")

End Sub

我在第二页上与之交互的元素是一个文本框。下面列出了调用的函数(工作正常)。

Function ClickButton(ieDoc As HTMLDocument, ByVal strButtonName As String)
Dim objInputs As Object, ele As Variant
Set objInputs = ieDoc.getElementsByTagName("input")

For Each ele In objInputs
    If ele.Title = strButtonName Then
        ele.Click
    End If
Next
End Function

如何将 ieDoc 重置为等于当前页面,以便能够与新页面上的文本框进行交互?在与文本框交互之前插入以下内容不起作用(我不知道为什么)。

set ieDoc = ie.document

非常感谢您提供的任何帮助!

【问题讨论】:

  • 加载新页面后再次Set ieDoc = ie.Document
  • @Comintern 不幸的是,这不起作用。我应该包括我已经尝试过的。
  • 你确定文本框总是在新页面上有这个ID吗? 00Nj0000009FpF9。也可以在这两个地方尝试Dim ieDoc as Object,看看会发生什么。
  • @ScottHoltzman 是的,文本框总是有 id。当我直接导航到包含它的页面时,编辑它的代码可以工作 - 如果我从另一个页面导航到该页面,它就不起作用。我尝试Dim ieDoc as Object,而不是子和函数中的HTMLdocument,但这并没有改变任何东西。
  • 对不起,我帮不上忙,我做了一些非常相似的事情,并且工作非常顺利,但不幸的是我不再参与那个项​​目:(

标签: html vba excel internet-explorer


【解决方案1】:

@dee 您已经竭尽全力回答这个问题。非常感谢!

您的回答不是为我解决了它,但这是我能够解决它的原因。你提到它可能是一个框架让我们(我和一个同事)意识到 IE 可能在框架完成加载之前完成了一个进程。

修复就像添加一样简单

Application.wait DateAdd("s", 5, Now)

之后

While ie.busy Or ie.ReadyState<> Readystate_complete: DoEvents: Wend

代码的最后一位现在读取

While ie.Busy Or ie.ReadyState <> READYSTATE_COMPLETE: DoEvents: Wend

Application.Wait DateAdd("s", 5, Now)

iedoc.getElementById("00Nj0000009FpF9").Value = "Place holder Text"

Call ClickButton(ieDoc, "Save")

这是一个不优雅的解决方案,但它可以完成工作。

再次感谢您的帮助。我将实施您所做的一些其他更改,以使我的代码更明确、更易于阅读。

【讨论】:

    【解决方案2】:

    我创建了两个页面,其中第一页包含按钮,第二页包含文本框。波纹管工作正常的代码,文本框被填充。所以这个简单的例子表明它应该可以工作。为什么它不适用于您的内部页面?不知道。难道你那里有一些IFrame

    ' Add reference to Microsoft Internet Controls (SHDocVw)
    ' Add reference to Microsoft HTML Object Library
    
    Sub AddInfoFromIntranet()
        Dim ie As SHDocVw.InternetExplorer
        Dim doc As MSHTML.HTMLDocument
        Dim url As String
    
        url = "file:///c:/My Web Sites/main.html"
        Set ie = New SHDocVw.InternetExplorer
        ie.Visible = True
        ie.navigate url
        While ie.Busy Or ie.readyState <> READYSTATE_COMPLETE: DoEvents: Wend
    
        Dim newServiceNoteButton As MSHTML.HTMLInputElement
        Set doc = ie.document
        Set newServiceNoteButton = doc.querySelector("input[title='New Service Note']")
    
        If (Not newServiceNoteButton Is Nothing) Then
            newServiceNoteButton.Click
            While ie.Busy Or ie.readyState <> READYSTATE_COMPLETE: DoEvents: Wend
            Set doc = ie.document
            Dim targetTextBox As MSHTML.HTMLInputElement
            Set targetTextBox = doc.getElementById("00Nj0000009FpF9")
            If Not targetTextBox Is Nothing Then targetTextBox.Value = "Placeholder Text"
        End If
        ie.Quit
        Set ie = Nothing
    End Sub
    

    首页main.html

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <!-- saved from url=(0014)about:internet -->
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <title>Untitled 1</title>
    </head>
    <body>
        <input type="button" title="New Service Note" value="Test" onclick="location.href='next.html';">
    </body>
    </html>
    

    第二页next.html

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <!-- saved from url=(0014)about:internet -->
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <title>Untitled 1</title>
    </head>
    <body>
        <input type="text" id="00Nj0000009FpF9">
    </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多