Link to home
Start Free TrialLog in
Avatar of CGWTax
CGWTax

asked on

How to I control an Internet Explorer Popup Window with VBA

Basically, I am trying to Use VBA Excel to put information from an Excel spreadsheet into a form in a webpage then click submit which brings up a table of information. When I click on a link on the table, it brings up a popup window with an image. I want to be able to switch control to the popup window, save the image (there is a save button on the popup window or I can use sendkeys), close the popup window and then continue the process. I am able to put the information into the table and click on the link to bring up the popup window, but I am unable to control the popup window. I do I switch the focus to the created popup window.
Avatar of Shahid Thaika
Shahid Thaika
Flag of India image

I have extensively worked on projects that require loading a web page as an object and manipulating it. Although I haven't done this type of a case, I am pretty sure we can do this using objects. Depending on how you are loading the original page - HTMLDocument Object, Web Browser Control, etc... the resultant popup can be found. Does the pop-up that opens have a name associated with it...

example: windowHandle = window.open([URL [, windowName [, features]]])

In the above case, I can search for "windowHandle" within my HTML Object and do button submits through that. Alternatively, you could try using APIs to get the Handle (hWnd) value of the pop-up window, given the title as a parameter and cycle through its objects to find the required button.

Two possible solutions, but I hope you have a good understanding of javascripts, else you may find this very difficult.
Avatar of CGWTax
CGWTax

ASKER

At the moment, I am using the following to load the web page. I am able to load the page and click on the link that brings up the popup window.

Set IE = CreateObject("InternetExplorer.Application")
With IE
     .Navigate {url}
...
End WIth

If there is another way of opening the initial page that would allow more flexibility going forward, I am willing to change it. I am able to identify the URL and name of the popup window by cycling through all of the open objects and outputting the locationurl and locationname. Once I have the name (or url) though, I am just not sure how to switch control to it. Is there any event that is triggered when a popup window opens that I could take advantage of? While I have done a lot of VBA programming, This is my first attempt at using VBA to control IE. Any suggestions would be much appreciated. Also, I tried using the webbrowser to load the page into a VBA form, but the window just comes up blank. Is there any library I need to reference to use it. I am using Excel 2007.
Hi - It doesn't matter whether you use CreateObject() or Web Browser Control, it's all the same objects anyway, and can be equally manipulated. If you are able to get the handle of the pop-up window, either cycling through or opening with a particular name, you can simulate window focus using the following javascript.


may be something like this??

objIE.Document.parentWindow.execScript("if (window.focus) {popupwindow.focus()}", "javascript")



Can you reply with the URL and pop-up link, or attach your code here. I'll see what I can do.
Hmm.. I did a cut, but forgot to paste back. My last few lines should read...

...you can simulate window focus using the following javascript.

if (window.focus) {popupwindow.focus()}

may be something like this??
objIE.Document.parentWindow.execScript("if (window.focus) {popupwindow.focus()}", "javascript")


Can you reply with the URL and pop-up link, or attach your code here. I'll see what I can do.
ASKER CERTIFIED SOLUTION
Avatar of Shahid Thaika
Shahid Thaika
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial