Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Events in InternetExplorer Object

Status
Not open for further replies.

hprx

Programmer
Nov 12, 2002
3
0
0
CA
Hi, I've been having problems getting NewWindow2 and BeforeNavigate2 to cancel in the following vb script. I've had the same problems with jscript. I'm not really sure why this doesn't work. Another question I had was how to unadvise on quit. I've been running this using wscript. Is there something wrong with this code? Thanks a lot.

Code:
Set IEobject = WScript.CreateObject("InternetExplorer.Application","IEobject_")
IEobject.Visible = True
IEobject.Navigate2 "[URL unfurl="true"]http://www.google.com"[/URL]

Do While IEobject.Visible
    WScript.Sleep 500
Loop

Sub IEobject_BeforeNavigate2(pDisp, URL, Flags, TargetFrameName, PostData, Headers, Cancel)
    Wscript.Echo "Going to:", URL
    Cancel = True
End Sub

Sub IEobject_NewWindow2(pDisp, ByRef Cancel)
    Cancel = True
End Sub
 
What are you trying to do with these subs? I copied your code and got rid of the Do While Loop and the program closed fine. BeforeNavigate2 also worked fine. _NewWindow2 did not though. Where did you look up these functions I'd be interested and looking at them.


Cheers

 
Um, it works? So during the beforenavigate2 event a messagebox shows up and then the navigation cancels? Because I've never gotten the navigation to cancel.

The reason the do while loop is in there is so it works past the first event. Otherwise, the script will stop working after the first event. At least, that's how it works on my present system...

I was trying to cancel navigations based on urls and whether it's in a new window or not. But, even though cancel is set to true, it just doesn't work for me and I can't find anything to suggest that this should not work.

The subs implement the internet explorer events which can be found at

Cheers.
 
Hey there,

Um, No sorry you are right the Cancel doesn't work. I've tried a few different methods. Can you get the OnQuit sub to run?
 
Yeah, OnQuit works. I'm just not quite sure how to unadvise. I'm thinking this problem might be memory oriented, but I'm not quite sure about it. The cancel variable doesn't appear to be written to, though I haven't tried debugging this yet.


 
I've been working this recently, and here is the answer, for all interested:
Here is how the MSDN articles say to use the NewWindow2 event:

Sub IEobject_NewWindow2(ByRef ppDisp as object, ByRef Cancel as Boolean)
Cancel = True
End Sub

You can't do this with VBScript!
Why? Variants! Here is the declaration as it appears in VBScript:

Sub IEobject_NewWindow2(ByRef ppDisp, ByRef Cancel)
Cancel = True
End Sub

Notice that the type declarations for the ByRef parameters are missing? VBScript handles all variables as type variant. You can't return ANY values to IE from your event handler in the ByRef parameters, because IE looks for a typed vairable, not a variant. All the events fire, but that's all.
You can get it to work in VBA. I put a command button on an empty MS Word document and the following code behind it:

Dim WithEvents IEVNT As InternetExplorer
'add a reference to Microsoft Internet Controlls, shdocvw.dll

Sub popblockedcmd_Click()
Set IEVNT = CreateObject("InternetExplorer.Application")
IEVNT.Visible = True
IEVNT.navigate ("about:home")
End Sub

Sub IEVNT_NewWindow2(ByRef ppdisp As Object, _
ByRef Cancel As Boolean)
Cancel = True
MsgBox "Popup Blocked"
End Sub

Private Sub IEVNT_OnQuit()
Set IEVNT = Nothing
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top