M
Mayhew
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
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