Results 1 to 7 of 7

Thread: Specified cast is not valid?

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2007
    Posts
    9

    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:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         BackgroundWorker1.RunWorkerAsync()
    5.     End Sub
    6.  
    7.     Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    8.         Dim Temp As Object
    9.         Temp = CreateObject("InternetExplorer.Application")
    10.         Temp.Visible = True
    11.         Temp.Navigate("http://www.google.com")
    12.         Do Until Temp.ReadyState = 4
    13.         Loop
    14.         'MsgBox(Temp.document.body.innerHtml) 'this works but parentwindow doesnt
    15.         Temp.document.parentWindow.execScript("alert('test')")
    16.     End Sub
    17. End Class

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: Specified cast is not valid?

    this is how your code should look with option strict on.

    vb Code:
    1. Dim Temp As SHDocVw.InternetExplorer
    2. Temp = DirectCast(CreateObject("InternetExplorer.Application"), SHDocVw.InternetExplorer)
    3. Temp.Visible = True
    4. Temp.Navigate("http://www.google.com")
    5. Do Until Temp.ReadyState = 4
    6. Loop
    7. 'MsgBox(Temp.document.body.innerHtml) 'this works but parentwindow doesnt
    8. DirectCast(Temp.Document, mshtml.IHTMLDocument2).parentWindow.execScript("alert('test')")

    edit: you need a reference to microsoft.mshtml.dll

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2007
    Posts
    9

    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.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: Specified cast is not valid?

    you need to add a reference to microsoft.mshtml.dll + shdocvw.dll

    i tested it. it works...

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2007
    Posts
    9

    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:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         BackgroundWorker1.RunWorkerAsync()
    5.     End Sub
    6.  
    7.     Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    8.         Dim Temp As SHDocVw.InternetExplorer
    9.         Temp = DirectCast(CreateObject("InternetExplorer.Application"), SHDocVw.InternetExplorer)
    10.         Temp.Visible = True
    11.         Temp.Navigate("http://www.google.com")
    12.         Do Until Temp.ReadyState = 4
    13.         Loop
    14.         'MsgBox(Temp.document.body.innerHtml) 'this works but parentwindow doesnt
    15.         DirectCast(Temp.Document, mshtml.IHTMLDocument2).parentWindow.execScript("alert('test')")
    16.     End Sub
    17. End Class

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: Specified cast is not valid?

    ok. you're right, it won't run in a backgroundworker thread.
    try this instead:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         Dim t As New Threading.Thread(AddressOf doWork)
    5.         t.SetApartmentState(Threading.ApartmentState.STA)
    6.         t.Start()
    7.     End Sub
    8.  
    9.     Private Sub doWork()
    10.         Dim Temp As SHDocVw.InternetExplorer
    11.         Temp = DirectCast(CreateObject("InternetExplorer.Application"), SHDocVw.InternetExplorer)
    12.         Temp.Visible = True
    13.         Temp.Navigate("http://www.google.com")
    14.         Do Until Temp.ReadyState = 4
    15.         Loop
    16.         'MsgBox(Temp.document.body.innerHtml) 'this works but parentwindow doesnt
    17.         DirectCast(Temp.Document, mshtml.IHTMLDocument2).parentWindow.execScript("alert('test')")
    18.     End Sub
    19.  
    20. End Class

  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2007
    Posts
    9

    Re: Specified cast is not valid?

    Thanks that worked.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width