|
-
Sep 2nd, 2010, 06:13 PM
#1
Thread Starter
New Member
Specified cast is not valid?
Hey, im trying to run this code and keep running into "Specified cast is not valid" for the line with Temp.document. etc.
It is allowing me to access all of IE's properties except for parentWindow. Im not sure what to do here. Does anyone have any ideas?
a Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim Temp As Object
Temp = CreateObject("InternetExplorer.Application")
Temp.Visible = True
Temp.Navigate("http://www.google.com")
Do Until Temp.ReadyState = 4
Loop
'MsgBox(Temp.document.body.innerHtml) 'this works but parentwindow doesnt
Temp.document.parentWindow.execScript("alert('test')")
End Sub
End Class
-
Sep 2nd, 2010, 06:55 PM
#2
Re: Specified cast is not valid?
this is how your code should look with option strict on.
vb Code:
Dim Temp As SHDocVw.InternetExplorer
Temp = DirectCast(CreateObject("InternetExplorer.Application"), SHDocVw.InternetExplorer)
Temp.Visible = True
Temp.Navigate("http://www.google.com")
Do Until Temp.ReadyState = 4
Loop
'MsgBox(Temp.document.body.innerHtml) 'this works but parentwindow doesnt
DirectCast(Temp.Document, mshtml.IHTMLDocument2).parentWindow.execScript("alert('test')")
edit: you need a reference to microsoft.mshtml.dll
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 2nd, 2010, 07:03 PM
#3
Thread Starter
New Member
Re: Specified cast is not valid?
The code you provided still gives me the same error.
Last edited by rejectpenguin; Sep 2nd, 2010 at 07:40 PM.
-
Sep 2nd, 2010, 07:49 PM
#4
Re: Specified cast is not valid?
you need to add a reference to microsoft.mshtml.dll + shdocvw.dll
i tested it. it works...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 2nd, 2010, 08:08 PM
#5
Thread Starter
New Member
Re: Specified cast is not valid?
Did you test it in the background worker because im still receiving the same error as before. I have included both mshtml and shdocvw.
vb Code:
Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click BackgroundWorker1.RunWorkerAsync() End Sub Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork Dim Temp As SHDocVw.InternetExplorer Temp = DirectCast(CreateObject("InternetExplorer.Application"), SHDocVw.InternetExplorer) Temp.Visible = True Temp.Navigate("http://www.google.com") Do Until Temp.ReadyState = 4 Loop 'MsgBox(Temp.document.body.innerHtml) 'this works but parentwindow doesnt DirectCast(Temp.Document, mshtml.IHTMLDocument2).parentWindow.execScript("alert('test')") End Sub End Class
-
Sep 2nd, 2010, 08:16 PM
#6
Re: Specified cast is not valid?
ok. you're right, it won't run in a backgroundworker thread.
try this instead:
vb Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim t As New Threading.Thread(AddressOf doWork)
t.SetApartmentState(Threading.ApartmentState.STA)
t.Start()
End Sub
Private Sub doWork()
Dim Temp As SHDocVw.InternetExplorer
Temp = DirectCast(CreateObject("InternetExplorer.Application"), SHDocVw.InternetExplorer)
Temp.Visible = True
Temp.Navigate("http://www.google.com")
Do Until Temp.ReadyState = 4
Loop
'MsgBox(Temp.document.body.innerHtml) 'this works but parentwindow doesnt
DirectCast(Temp.Document, mshtml.IHTMLDocument2).parentWindow.execScript("alert('test')")
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 3rd, 2010, 01:16 AM
#7
Thread Starter
New Member
Re: Specified cast is not valid?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|