0

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
Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
Joel Bastien
  • 121
  • 2
  • 11
  • you can automate logging in – QHarr Mar 12 '19 at 20:37
  • Right, I understand this. However, I won't be the only one using the macro and therefore wouldn't be able to automate signing in others as well for security reasons. – Joel Bastien Mar 12 '19 at 21:19
  • Op created a [Duplicate Thread] for the same issue. Ref: https://stackoverflow.com/questions/55164307/how-to-navigate-to-an-already-opened-ie-window-using-vba – Deepak-MSFT Mar 15 '19 at 02:31

2 Answers2

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:

enter image description here

Reference:

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

Deepak-MSFT
  • 10,379
  • 1
  • 12
  • 19
  • Hi, thank you so much for your reply! This seems like it may work, however, my problem is that my webpage doesn't seem to have a document title. I'm not too familiar with html, so I'm not sure what should be my input for the "XYZ" portion of the VBA code. If I inspect element in the website, this is what I find for the "title portion" .... Any idea? – Joel Bastien Mar 13 '19 at 19:30
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")
Noodles
  • 194
  • 1
  • 4
  • Great thank you so much! I'm not quite sure how this works entirely. However, whenever I try to run the code, it gives me an error: Object doesn't support the property or method. Any idea why that is, I'm sure I'm missing something here or need to input something that may be completely obvious that I've missed! – Joel Bastien Mar 12 '19 at 21:34