0

I regularly navigate an intranet website that opens a second window after clicking a link on the first. I would like to change the value of a "textarea" element in the second window but have problems focusing on the correct window. Are there any suggestions on how I should continue the following code to achieve what I want to do?

IE.Navigate webPageURL

PageLoadWait 'sub that checks IE.busy and IE.readystate

With IE.Document.Frames("topFrame").document

   .getElementById("user")=userID

   .getElementById("pwd")=password

   .getElementById("submit"). Click

End With

PageLoadWait 'Second window opened

I looked at the question below but am not sure how I would be able to adapt it to my needs since window title is the same for both windows. I tried matching window URL since they are slightly different but was unable to succeed for some reason.

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

FaneDuru
  • 38,298
  • 4
  • 19
  • 27
Cake
  • 35
  • 5
  • But, do you know the second window URL? I mean, if not, can you deduce it starting from the first one...? – FaneDuru Aug 07 '20 at 10:11
  • If you are just looking to interact with the second window, you could use `hwnd` to do that. There are a few examples when you do a search for **hwnd** and **VBA** – Zac Aug 07 '20 at 10:24

1 Answers1

0

Supposing that you know the second window URL, try the next function, please:

Private Function SecIE(strURL As String) As InternetExplorer
    ' It needs a reference to 'Microsoft Internet Controls'
    Dim objIE As Object
    For Each objIE In CreateObject("Shell.Application").Windows
        If objIE.Name = "Internet Explorer" Then
            If objIE.Document.Location = strURL Then
                Set SecIE = objIE: Exit Function
            End If
        End If
    Next
End Function

It can be called in this way:

Sub testFindIEObject()
   Dim secondIE As InternetExplorer
   Set secondIE = SecIE("http://www.msn.com/en-xl/?ocid=iehp&AR=1") 'use your sec Wind URL
   Debug.Print secondIE.Document.Title
End Sub

I used he above URL only for testing reasons...

If you do not know, are not able to extract the second window URL, I think I can adapt the code to find he second one, starting from its title. A static variable must be created and count the windows with the same title. The first window will be skipped and the second defined like the wished IE object.

FaneDuru
  • 38,298
  • 4
  • 19
  • 27
  • The second window has frames too, and each frame has a different URL. Which URL should I then use? – Cake Aug 07 '20 at 23:04
  • @Cake: Are you talking about an Internet Explorer window, as you asked? Such a window could have only a unique URL... You can find the document frames, as your piece of code does. Am I missing anything? – FaneDuru Aug 09 '20 at 09:05
  • My understanding of how things work is probably insufficient, leading to this question. Yes, the second internet explorer window has three frames, and when I right clicked for the properties at a location on each of the frames, a different URL was given. The URL on the address bar is the same on both the first and second windows, and so is the title. – Cake Aug 10 '20 at 08:27