How to navigate to an already opened IE window usi

2019-08-19 17:27发布

问题:

So I've been trying to navigate to an already opened IE window using VBA. I've found a post explaining what seems to be how to do this. However, my main problem now is that I'm not too familiar with VBA and my website in particular, doesn't seem to have a document title. All I have in the html code when i inspect it in the Debugger is the following:

<title></title>

Am I missing something here? Is there any other way to refer to the website that may be easier or simpler?

Sub demo()

Dim str_val As Object
marker = 0
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For x = 0 To (IE_count - 1)
    On Error Resume Next
    my_url = objShell.Windows(x).document.Location
    my_title = objShell.Windows(x).document.Title

    If my_title Like "XYZ" & "*" Then
        Set IE = objShell.Windows(x)
        marker = 1
        Exit For
    Else
    End If
Next

If marker = 0 Then
 MsgBox ("A matching webpage was NOT found")

Else
    MsgBox ("A matching webpage was found")

    Set str_val = IE.document.getElementById("txtbox1")
    str_val.Value = "demo text"

End If
End Sub

Reference

VBA code to interact with specific IE window that is already open

回答1:

This is the way I normally handle said problem,

Sub GetURLs()

'   Uses MS HTML Controls
'   and MS Internet Controls

Dim s As SHDocVw.InternetExplorer
Dim sw As SHDocVw.ShellWindows
Dim d As MSHTML.HTMLDocument

Set sw = New SHDocVw.ShellWindows

For Each s In sw
    If TypeName(s) = "IWebBrowser2" And s.Name <> "Windows Explorer" Then
        Debug.Print s.Name, s.LocationURL
        Set d = s.Document
        Debug.Print d.getElementsByTagName("TD")(0).innerhtml
    End If
Next s

Set sw = Nothing
Set s = Nothing

End Sub