Internet Explorer Object | Pop up Window

gduron

Board Regular
Joined
Mar 27, 2006
Messages
94
Hello,

I have a VBA project that navigates to a website and then clicks on a link. When I click the link in my code a new IE Browser opens with a form that I wan to fill out. I know how to update the form, but I don't know how to reference the new browser window that was opened when I clicked on the link.

Here is some of my code:

Code:
'Open End User Multiple Selection
Set tags = myIE.document.getElementsByTagName("A")
For Each tag In tags
    If InStr(1, tag, "'multiple_selections.htm','EndUser'", vbTextCompare) > 0 Then
        tag.Click
        Exit For
    End If
Next

when I try to reference the document in the popup window, it is still referencing the previous window. How can I set my "myIE" object to reference the new pop up window?

Thanks in advance for your help.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Internet Explorer | Open Window

Hello,

I posted a similar message to this not long ago, but I didn't receive any responses. I hoped by restating my problem and giving more detail, somebody might have an idea.

Currently I have a list in excel that I want to populate in an Internet Explorer Form. I have already coded into my macro the ability to open internet explorer, logon to the website where the form is located. Once on the website, I need to click a link that opens a new browser window where the form is. Unfortumately my internet explorer object still references the other open window that contains the link that opens the browser form. How do I create a new Internet Explorer object that will reference the pop up object browser. Here is a snippet of my code to date:

Code:
'Variable declarations
Dim myIE As New InternetExplorer 'New '
Dim myURL As String
Dim myDoc As HTMLDocument

myURL = "https://..."


myIE.navigate myURL
myIE.Visible = True

Do While myIE.Busy Or myIE.readyState <> READYSTATE_COMPLETE
DoEvents
Loop

Set myDoc = myIE.document

myDoc.forms(1).loginid.Value = userName
myDoc.forms(1).password.Value = password
myDoc.form1.submit

Do While myIE.Busy Or myIE.readyState <> READYSTATE_COMPLETE
DoEvents
Loop

'Open End User Multiple Selection
Set tags = myIE.document.getElementsByTagName("A")
For Each tag In tags
    If InStr(1, tag, "'multiple_selections.htm','EndUser'", vbTextCompare) > 0 Then
        tag.Click
        Exit For
    End If
Next


The problem happens after the last for loop because the variable "myIE" still references the old web page with the link that I just clicked to open the form. I need to now create an object that references that new form and I can't figure how to do it.

Thanks in advance for your help.
 
Upvote 0
Re: Internet Explorer | Open Window

Must you click the link and open a new browser?

Why don't you just navigate to it, just as you navigated to the original URL?

You should be able to extract the relevant URL from the relevant tag.
 
Upvote 0
Because after I open the form, there is a submit button that takes me back to the previous page. If I try to navigate to the URL the form displays in Internet Explorer, but then when I click the submit button, it doesn't take me back to the previous page which I need to be on to take further action. Instead, after you press the submit button the previous page does not load and says there was an error.
 
Upvote 0
I'm afraid I don't quite understand what you mean.

If you have the relevant URLs then you should be able to navigate wherever you want.

Mind you if you are dealing with a secure site that you need to log into you might need to do that multiple times.

By the way you really shouldn't need to use click, if what you are submitting is a form then use it's submit method.
 
Upvote 0
I'm aware of that. But when you submit the form, it actually just stores the information. After you have submitted the form, you then need to choose more options on the previous page. Once all this is done, you submit that page and it uses the information in that form to do some processing of the data.

Do you know of any way to reference an Internet Explorer window that is already open without navigating to it? That would actually solve my problem completely.
 
Upvote 0
Oh. So another reason why you can't navigate to it is because the URL tracks some sort of session variable inside it. So if you navigate separately to the form, the session of that form is not tracked by the browser that needs the information from the form. Any other ideas?
 
Upvote 0
Re: Internet Explorer Object | Pop up Window Control - Solution

Ok, well thanks to some diligent searching on the internet, I found a solution to the problem. If you are ever using Excel VBA to click on a link and it opens a pop-up window, your internet explorer object will continue to reference the parent browser and not the child browser. I found this code on another site to help you create an object that will allow you to manipulate the child or pop-up browser:

Code:
Dim objSW As ShellWindows
Dim objIE As InternetExplorer

Set objSW = New ShellWindows
For Each x In objSW
        If x.LocationName = "Multiple Selection" Then
            Set objIE = x
            Exit For
        End If
Next x

The ojSW will contain all of the open windows on your computer (as objects of course). You can iterate through each one of them until you find the "LocationName" of the window that you are interested in. Once you find it, set your new IE variable equal to that object and you can keep playing with it.

Thanks for your help.
 
Upvote 0
Hi John,

Thanks for your reply. I'm really interested in learning more about WithEvents. I tried to implement the code in http://www.mrexcel.com/forum/showthread.php?t=345033. When I tried to add the following line of code to a regular module I received the following error: "only valid in object module".

Code:
Public WithEvents IE1 As InternetExplorer

Does this code have to be implemented in a Class module or is there a workaround?
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,449
Members
448,966
Latest member
DannyC96

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top