How do I trap InternetExplorer events in VBA

I want to trap several events associated with shDocVw.InternetExplorer.

At first I tried to declare an Internet Explorer object variable in the declarations section of a standard module as follows:

Public WithEvents Explorer As SHDocVw.InternetExplorer

This got me the error message "Only valid in object module", highlighting the "WithEvents" statement. So I created a class module instead, "clsMyExplorer", and typed the following in the declarations section:

Public WithEvents myExplorer As shdocvw.InternetExplorer

I then tried to create an instance of this class in a standard module:
Declarations Section: Public myIE As clsMyExplorer
Sub DoThis(): Set myIE.myExplorer = shdocvw.InternetExplorer

However it doesn't work, but I'm sure I'm almost there. Can someone help?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hello TimHudspith,

you could try something like
in a classmodule

Option Explicit

Public WithEvents myExplorer As SHDocVw.InternetExplorer

Private Sub myExplorer_NavigateComplete2(ByVal pDisp As Object, URL As Variant)
  MsgBox "ready"
End Sub

in a module

Option Explicit

Public myIE As clsMyExplorer
Sub DoThis()
  Set myIE = New clsMyExplorer
  Set myIE.myExplorer = New SHDocVw.InternetExplorer
  myIE.myExplorer.Visible = True
  myIE.myExplorer.Navigate ("")

End Sub


worked for me in Excel 2000

hope this helps a bit and a happy new year!

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TimHudspithAuthor Commented:
Spot on. Cheers
thanks for the grade, glad to help :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.