How to navigate to an already opened internet expl

2019-08-17 22:44发布

问题:

so I've been searching around the web a lot on how to do this and can't seem to find any concrete information on this. I've seen a lot of examples on how to open a new internet explorer window and navigate to a specific website. However, in my particular case, I want to be able to simply navigate to an already opened internet explorer window (it would be the only window/tab open of internet explorer). The reason for this is because every time I open the website, I need to log in before being able to do anything. I would then ideally like to paste some ID's into a search box and press enter (I should be able to find out how to do this part through searching online).

Here is what I've found so far, but I'm a little lost and not sure how I would apply this bit of code to work as I would like it to.

Sub ExplorerTest()



Const myPageTitle As String = "Wikipedia"
Const myPageURL As String = "http://en.wikipedia.org/wiki/Main_Page"
Const mySearchForm As String = "searchform"
Const mySearchInput As String = "searchInput"
Const mySearchTerm As String = "Document Object Model"
Const myButton As String = "Go"
Dim myIE As SHDocVw.InternetExplorer


With myIE.Document.forms(mySearchForm)
    'enter search term in text field
    .elements(mySearchInput).Value = mySearchTerm
    'press button "Go"
    .elements(myButton).Click
  End With

End Sub

回答1:

This does all browser windows both Internet Explorer and Windows Explorer

Window is a Internet Explorer Window object.

Set objShell = CreateObject("Shell.Application")
Set AllWindows = objShell.Windows
For Each window in AllWindows
    msgbox window.location
Next

Or if you are sure it's the only one open

Set x = GetObject(,"InternetExplorer.Application")


回答2:

I try to check your description and I find that you want to get object of already opened IE window and than try to automate it.

You can try to refer example below may help you.

In this example, You can see that already opened page get searched using it's title. Than you can use it's object to automate that page.

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

Output:

Reference:

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