Getting Information about Events Seen In the Object Browser

  • Thread starter gimme_this_gimme_that
  • Start date
G

gimme_this_gimme_that

Events are designated in the Object Dictionary by a lightening icon.

Is there a way to tell if an Event has fired?

Example.

Check the "Microsoft Internet Controls" reference library and check
out the WebBrowser object - it has an event called DownloadComplete.

So if I have:

Set IE = CreateObject("InternetExplorer.Application")
' Same as Set IE = new WebBrowser()
IE.Visible = True
IE.navigate "http://www.mysite.com"

Can I see if DowloadComplete fired?

Can I attach code that executes when DownloadComplete fires?

BTW, I see there is a Busy property which can be used to see when the
download is complete - but I'd still like to learn about capturing
events.


Thanks.
 
T

Tim Williams

You can declare an object variable "With Events" - this allows you to attach
to the object's events.

Yim
 
G

got.sp4m

Here's an exmaple using the BeforeNavigate2 and NavigateComplete2
events;

In a class module named "clsIEEventHandler":

Option Explicit

Public WithEvents InternetExplorer As SHDocVw.InternetExplorer

Private mboolNavIsComplete As Boolean

Friend Property Get NavigationIsComplete() As Boolean
NavigationIsComplete = mboolNavIsComplete
End Property

Private Sub Class_Terminate()
Set Me.InternetExplorer = Nothing
End Sub

Private Sub InternetExplorer_BeforeNavigate2(ByVal pDisp As Object, _
ByRef URL As Variant, ByRef Flags As Variant, _
ByRef TargetFrameName As Variant, ByRef PostData As Variant, _
ByRef Headers As Variant, ByRef Cancel As Boolean)
'Fires before navigation
mboolNavIsComplete = False
End Sub

Private Sub InternetExplorer_NavigateComplete2(ByVal pDisp As Object,
_
ByRef URL As Variant)
'Fires when navigation is complete
MsgBox "Navigation completed"
mboolNavIsComplete = True
End Sub


In a standard code module:

Option Explicit

Sub TestIEEventHandler()
Dim objIE As SHDocVw.InternetExplorer
Dim objIEEvents As clsIEEventHandler

Set objIE = New SHDocVw.InternetExplorer
Set objIEEvents = New clsIEEventHandler
Set objIEEvents.InternetExplorer = objIE

With objIE
.Visible = True
.Navigate "http://www.DailyDoseOfExcel.com"
End With

'Ensure that navigation completes before
'the event handler is destroyed
Do While Not objIEEvents.NavigationIsComplete
DoEvents
Loop

'Destroy objects
Set objIEEvents = Nothing
Set objIE = Nothing
End Sub


As you see in the example you can use a class module to trap events of
objects. As Tim says use the With Events statement.. The
DowloadComplete even probably fires when a file has been downloaded
(just guessing, I'm not familiar with the InternetExplorer object
model.

best regards
Peder Schmedling
 

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