导航到VBA中的页面后无法重置HTML文档

我正在使用VBA导航IE,并无法重置文档对象,以便可以导航多个页面。 我用这个问题的答案来帮助创build原始脚本,但却遇到了一个类似海报的问题。 不幸的是,列出的答案没有帮助或指导我解决scheme。

我正在使用下面的代码来拉动页面并导航。

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 

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

我创build了两页,第一页包含button,第二页包含文本框。 贝娄工作正常的代码,文本框被填充。 所以这个简单的例子表明它应该工作。 为什么它不适合你的内部页面? 不知道。 难道你在那里有一些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> 

@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") 

这是一个不合理的解决scheme,但却能完成这项工作。

再次感谢你的帮助。 我将执行一些其他更改,以使我的代码更加清晰易读。