none
Error when trying to access html web page with frames while using SHDOCVW internet explorer. RRS feed

  • Question

  • Hello. I am writing with vb.net using visual studio (both express and professional 2015 version). I import references to SHDOCVW.dll and mshtml.dll and then i create an IE new object like this: (i want to use the object and not the common web browser control of visual basic..)

    Dim WithEvents IntE As SHDocVw.InternetExplorer

    IntE3 = New SHDocVw.InternetExplorer
    IntE3.Visible = True
    IntE3.Navigate("web page")

    The web page i want to access has a frameset instead of a <body> tag, which has two frames in it, the first frame has a different src with the ending ".do" and inside that frame are all the components i want to read their values or write to them.

    I normally access this web page with: IntE3.Document.Frames(0). Document.forms(0).specifiedtextbox.Value, this code works fine for vb6. But with vb.net when i try to get

    IntE3.Document.Frames.length i get an error: "the specified cast is not valid". I can't even use Document.Frames even though frames is a property of Document.


    I also tried:

    Dim HTMLDoc As mshtml.IHTMLDocument

    HTMLDoc = IntE3.Document

    Dim f1 as mshtml.IHTMLWindow2 = HTMLDoc.frames.item(0) is not accepted either nor Dim f1 as mshtml.IHTMLWindow2 = CType(HTMLDoc.frames.item(0),mshtml.IHTMLWindow2)

    while:

    Dim frame As mshtml.HTMLFrameElement
    frame = IntE3.Document.getElementsByTagName("frame").item(0)
    it seems that i get the frame element i want but i can't go any further and see the contents of it.

    I've tried all kind of approaches i found online in forums or tutorials but nothing seems to be working. I must say that using the webbrowser control instead, seems to be working fine but i then have compatibility issues and i would definitely prefer to use the object to create an instance of explorer.

    I cannot understand why the web browser control is working or why vb6 with the exact same code is working fine as well.

    Thanks for reading my problem,

    Yvonni.

    Thursday, June 29, 2017 9:02 AM

All replies

  • You don't write what kind of error, therefore let me guess 

    "Did you connect to Internet?"


    Success
    Cor

    Thursday, June 29, 2017 11:18 AM
  • As i mentioned above, the error i get is "Specified cast is not valid". In particular:

    "An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll

    Additional information: Specified cast is not valid."

    I do connect to internet, when i type on the immediate pane ?IntE i get :

    "{SHDocVw.InternetExplorerClass}
        SHDocVw.InternetExplorerClass: {SHDocVw.InternetExplorerClass}"

    and when i type ?IntE.document i get in response:

    {[object]}
        mshtml.HTMLDocumentClass: {[object]}

    but when i type ?IntE.document.frames or ?IntE.document.frames.length i get "Specified cast is not valid".

    Thursday, June 29, 2017 11:41 AM
  • Also i know for a fact that the frame has been loaded because i use the same code with vb6 and with vb.net web browser control, both return to me the actual number of frames and their contents.
    Thursday, June 29, 2017 11:43 AM
  • The easiest way is to put in top of your code 

    Option Strict Off 

    If it then goes, then you can look using the debugger what type is used and cast it to that. 

    CType(yourobject,TheObjectToCast)

    And then put on again Option Strict to avoid side effects.


    Success
    Cor

    Thursday, June 29, 2017 12:15 PM
  • I always use option strict off. And i don't actually try to cast anything, i want to see the frames and it seems the debugger can see no frames, so it could not give me the type that is used.
    Thursday, June 29, 2017 12:41 PM
  • Yea but this means really that you cast somewhere wrong. 

    Specified cast is not valid


    I tried the Shdocvw not for about 12 years

    However, the problem can be in MSHTML, you can try to set in Msthml properties the Embeded Interop type to false

    Please tell if that solves the problem?


    Success
    Cor



    Thursday, June 29, 2017 1:10 PM
  • Thank you for your help..

    No, unfortunately that did not solve the problem.

    I set in Mshtml properties the Embed Interop type as false and for Shdocvw properties i tried setting the Empeded Interop type both false and true, none made any difference.

    Thursday, June 29, 2017 1:45 PM
  • Are you accessing IntE3.Document.Frames.length in a DocumentComplete event?


    Sam Hobbs
    SimpleSamples.Info

    Thursday, June 29, 2017 11:48 PM
  • At first i used a timer after the document complete event and i tried to access it after the timer event had triggered. But i also tried accessing it from inside the Explorer's DocumentComplete event. Nothing changed.


    Friday, June 30, 2017 8:37 AM
  • Maria,

    It becomes a long thread, but you haven't shown one word of code. 

    We cannot see through your eyes you know. 


    Success
    Cor

    Friday, June 30, 2017 9:11 AM
  • Well, as i mentioned at my first post, my code is: (inside the IntE3.documentcomplete)

    IntE3 = New SHDocVw.InternetExplorer
    IntE3.Visible = True
    IntE3.Navigate("web page")

    Dim HTMLDoc As mshtml.HTMLDocument

    HTMLDoc = IntE3.Document

    and then

    dim I as integer = HTMLDoc.frames.length not working

    Dim f1 as mshtml.IHTMLWindow2 = HTMLDoc.frames.item(0) not working

    Dim f1 as mshtml.IHTMLWindow2 = CType(HTMLDoc.frames.item(0),mshtml.IHTMLWindow2) not working.

    The web page html has:

    <html>

    <head>

    <frameset>

      <frame1>

         content

      <frame2>

        content

    </frameset>

    That is all the code i have, if more code is necessary then obviously something is missing and that's why i need help.

    Thank you for your time but you probably have never faced the problem i am dealing with.


    Friday, June 30, 2017 9:22 AM