HTMLDocument - Type Mismatch

Posted on 2005-04-15
Last Modified: 2008-03-10
I am painfully trying to navigate a site and this is now a blocking problem.

I want to read some code from some JavaScript, and I get a Type Mismatch error when I try to call the procedure that is intended to read the source data,  using the document object as a parameter.

I test the type before calling the procedure, but it still produces an error.

This is the calling code:

Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)

    On Error GoTo DocumentCompleteError
                        WriteLog "GetSiteList WebBrowser1.document.frames(0).document type: " & TypeName(WebBrowser1.document.frames(0).document)
                        GetSiteList WebBrowser1.document.frames(0).document
WriteLog "DocumentCompleteError: " & Err.Number & ", " & Err.Description
    Select Case Err.Number
        Case 0
        Case 438
            Sleep 1000
        Case 13
        Case Else
            Resume Next
    End Select
End Sub

This is the called code

Sub GetSiteList(Doc As HTMLDocument)
    Dim sc As HTMLHtmlElement
    Dim ScriptLines
    Dim i As Integer
    Dim j As Integer
    Dim p As Integer
    Dim q As Integer
    On Error GoTo GetSiteListError
    WriteLog "In GetSiteList: " & TypeName(Dic)
    WriteLog "GetSiteList. Element count: " & Doc.All.length
    For Each sc In Doc.All
        If sc.tagName = "SCRIPT" Then
            WriteLog "GetSiteList. Script found"
End Sub

This is the logging code:

Sub WriteLog(Text As String)
    Dim f As Integer
    Dim strFileName As String
    strFileName = strLogPrefix & Format$(Now, "yy") & Format$(Format$(Now, "y"), "000") & ".log"
    Text = Format$(Now, "HH:nn:ss") & " " & Text
    Debug.Print Text
    f = FreeFile
    Open App.Path & "\" & strFileName For Append As #f
        Print #f, Text
    Close #f
End Sub

and this is what gets logged.

16:20:16 GetSiteList WebBrowser1.document.frames(0).document type: HTMLDocument
16:20:16 DocumentCompleteError: 13, Type mismatch

There is no log data from the called procedure, so the error must be in the call, but my log reports that the object type is what  the Sub is expecting. Help!
Question by:GrahamSkan
    LVL 32

    Expert Comment

    The DocumentComplete event will fire once for each frame in the document, and then once at the end of the entire document.  If you need to access the entire document and ignore the event during the loading of subframes, use:

    Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
        If (pdisp Is WebBrowser1.Object) Then
            'The entire document has loaded
    LVL 76

    Author Comment

    Thanks Erick37. I do have that line in the code.The problem is that I actually read the object type just before the call. It is reported as HTMLDocument, but still gives an error.
    LVL 8

    Accepted Solution


    Sub GetSiteList(Doc As IHTMLDocument2)

    TypeName is an unreliable way to determine type since it just returns a class name.  Having the same class name is not a guarantee of which interfaces are implemented. Also the default interface for HTMLDocument is a DISP interface.  I don't think VB can use it.


    LVL 76

    Author Comment

    Thanks for that Anthony.
    I did find that that

    TypeOf Doc Is HTMLDocument

    returned false, so I just called but I don't know what other objects to test it against. I could get past the error by decaring it as Object type, but then I was into guessing what properties and methods were available.

    I'll try your recommendation.
    LVL 8

    Expert Comment

    You can find out what is available from here.

    The Disp interface (the one used by scripts in the browser) is simply a collection of all the members from IHTMLDocument2, 3, 4, 5 and IHTMLDOMNode, 2.  

    For what you are doing IHTMLDocument2 is what you want.  For Each through Doc.scripts collection will be faster than using all.

    I'm not sure innerText will give you access to what you want.  If not then the IHTMLScriptElement interface has a text property and that will.

    On the hand you can use object every where and use the above documentation as a guide to what you can do.


    LVL 76

    Author Comment

    Thanks for that additional advice. Saturday evening has started here, so I'll try that tomorrow.
    LVL 76

    Author Comment

    Thanks very much

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
    When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now