HTMLDocument - Type Mismatch

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!
LVL 77
Who is Participating?
anthonywjones66Connect With a Mentor Commented:

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.


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
GrahamSkanRetiredAuthor Commented:
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.
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

GrahamSkanRetiredAuthor Commented:
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.
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.


GrahamSkanRetiredAuthor Commented:
Thanks for that additional advice. Saturday evening has started here, so I'll try that tomorrow.
GrahamSkanRetiredAuthor Commented:
Thanks very much
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.