Bryce Draper Bryce Draper - 1 month ago 15
HTML Question

Unable to reset HTML document after navigating to page in VBA

I'm using VBA to navigate IE and am having trouble resetting the document object so that I can navigate through multiple pages. I used the answer to this question to help create the original script, but am running in to a very similar problem that poster ran in to. Unfortunately, the answer listed does not help or guide me to a solution.

I'm using the following code to pull the page and navigate.

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


The element I'm interacting with on the second page is a text box. The function called (which works fine) is listed below.

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


How do I reset ieDoc to equal the current page so that I am able to interact with the text box on the new page? Inserting the following before interacting with the text box does not work (and I have no idea why).

set ieDoc = ie.document


Thank you very much for any help you're able to offer!

dee dee
Answer

I have created two pages where the first page contains the button and the second page contains the text box. Bellow the code which works fine, the text box is filled. So this simple example shows that it should work. Why is it not working with your internal page? No idea. Could it be that you have some IFrame there?

' 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

First page 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>

Second page 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>