Link to home
Start Free TrialLog in
Avatar of GrahamSkan
GrahamSkanFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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
'...
DocumentCompleteError:
WriteLog "DocumentCompleteError: " & Err.Number & ", " & Err.Description
    Select Case Err.Number
        Case 0
        Case 438
            DoEvents
            Sleep 1000
            Resume
        Case 13
            Stop
            Resume
        Case Else
            Stop
            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!
Avatar of Erick37
Erick37
Flag of United States of America image

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
Avatar of GrahamSkan

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of anthonywjones66
anthonywjones66

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of anthonywjones66
anthonywjones66

You can find out what is available from here.

http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/obj_document.asp

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.

Anthony.



Anthony.
Thanks for that additional advice. Saturday evening has started here, so I'll try that tomorrow.
Thanks very much