Access popup window from IE object.

G

Guest

I'm trying to get a reference to the InternetExplorer object that pops up
when I use VBA to fill out a form and click a link. I found the "NewWindow2"
event, but it doesn't seem to be doing what I want.

The NewWindow2 event fires when it should, but the "ppDisp" variable is set
to Nothing when I get into the function.

Any ideas what's going wrong?

Thanks,
Mayhew

Here's the code:
======================
Class Module IEClass:
======================
' class module "ieclass"
Public WithEvents x As InternetExplorer
Public y As InternetExplorer

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal
lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA"
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam
As Long) As Long

Private Sub x_NewWindow2(ppDisp As Object, Cancel As Boolean)
Set y = ppDisp
End Sub

Public Sub SetVisible(visible As Boolean)
x.visible = visible
End Sub


Public Sub Navigate(destURL)
x.Navigate2 destURL
LoadPage
End Sub

Public Sub LoadPage()
' Pauses execution until the browser window has finished loading
Do While x.Busy Or x.ReadyState <> READYSTATE_COMPLETE
PostMessage FindWindow("#32770", "Microsoft Internet Explorer"),
&H10, 0&, 0&
DoEvents
Loop
End Sub


Public Function Button_name(tagType, Caption As String) As Boolean
' Clicks the element of type tagType containing Caption or returns false if
element cannot be found
Dim Element

Button = True

Dim AllElements
Set AllElements = x.Document.getElementsByTagName(tagType)

For Each Element In AllElements
tempAlt = Element.Name
If InStr(Element.Name, Caption) > 0 Then
Call Element.Click
Call LoadPage
Exit Function
End If
Next Element
Button = False
End Function

==================
Module 'Module1'
==================
Sub URL_Test2()
Dim ie1 As New IEClass
Set ie1.x = New InternetExplorer
ie1.SetVisible True

Dim varURL As String
varURL = "http://www.weather.gov/climate/index.php?wfo=box"
ie1.Navigate varURL

With ie1.x.Document.forms(2)
.Product(1).Click
.station.Options(2).Selected = True
.recent(1).Click
.Date.Options(0).Selected = True
End With
successful = ie1.Button_name("img", "goMain")
breakPointHere = True 'for breakpoint.

End Sub
 
T

Tim Williams

This function will return a reference to an IE window if you know the URL (uses "like" so modify if you know the exact URL)


'*************************
Function GetIE(sLocation As String) As Object

Dim objShell As Object, o As Object
Dim sURL As String
Dim retVal As Object

Set retVal = Nothing
Set objShell = CreateObject("Shell.Application")

For Each o In objShell.Windows
sURL = ""
On Error Resume Next
sURL = o.document.Location
On Error GoTo 0
'debug.print sURL
If sURL Like sLocation & "*" Then
Set retVal = o
Exit For
End If
Next o

Set GetIE = retVal

End Function
'**************************
 
G

Guest

This is somewhat helpful. However, I still have a very similar problem.

After the button push, this function returns "Nothing". If I put a
breakpoint after the popup is activated, and wait for it to load, this
function works.

I can't wait do the ie2.Busy test to wait for the popup to finish loading,
because the ie2 object doesn't appear to exist yet, is there a way to pause
execution until that function will work?
 
T

Tim Williams

You could just Wait for the window to finish loading - if you know there
should be a window with the expected URL then you can just keep looking
until it appears (in a do...while loop or similar)

'********************
'submit the form then try to find the new window......

Dim ie2 As Object
Do
'have to wait until it's ready...
Application.Wait (Now + TimeValue("0:00:03"))
Set ie2 = GetIE("http://someserver.com/output/results.html")
Loop While ie2 Is Nothing
Debug.Print "Got new window"
'do stuff with ie2
'********************

Tim
 
G

Guest

That worked great. Thanks a bunch!

Tim Williams said:
You could just Wait for the window to finish loading - if you know there
should be a window with the expected URL then you can just keep looking
until it appears (in a do...while loop or similar)

'********************
'submit the form then try to find the new window......

Dim ie2 As Object
Do
'have to wait until it's ready...
Application.Wait (Now + TimeValue("0:00:03"))
Set ie2 = GetIE("http://someserver.com/output/results.html")
Loop While ie2 Is Nothing
Debug.Print "Got new window"
'do stuff with ie2
'********************

Tim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top