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
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
Re: Specified cast is not valid?
The code you provided still gives me the same error.
Re: Specified cast is not valid?
you need to add a reference to microsoft.mshtml.dll + shdocvw.dll
i tested it. it works...
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
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
Re: Specified cast is not valid?