+ Reply to Thread
Results 1 to 7 of 7

getElementById run-time 13 type mismatch error

Hybrid View

  1. #1
    Registered User
    Join Date
    02-19-2013
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2013
    Posts
    33

    getElementById run-time 13 type mismatch error

    Hi everyone,

    I have the following simple code that works well on my computer (Windows 7, Excel 2013, IE11) but not on my friend's computer (Windows 7, Excel 2010, IE11). The code errors on the getElementById line. Whenever "getElementById" appears anywhere in the full code, we get a run-time error 13 (type mismatch) on his computer. I've tested getElementsByClassName in the same position and it works fine. It seems like a strange, simple error but haven't found anyone on the forums who's come across this before. Can anyone generate this error? Microsoft HTML Object Library and Microsoft Internet Controls are references. Any help would be appreciated.

    Cheers, Rob

    Sub getElementByIdIssue()
    
    Dim IE As InternetExplorer
    Set IE = New InternetExplorer
    
    IE.Visible = False
    IE.Navigate ("http://www.racingandsports.com.au/en/form-guide/")
    
    Do
    DoEvents
    Loop Until IE.readyState = READYSTATE_COMPLETE
    
    Dim Doc As HTMLDocument
    Set Doc = IE.document
    
    Dim CountryHTML As String
    CountryHTML = Doc.getElementById("accordion36").innerHTML
    
    Range("A1") = CountryHTML 'added this line here just to test it's working
    
    IE.Quit
    
    '...(pull out various bits of data in the full code)
    
    End Sub

  2. #2
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: getElementById run-time 13 type mismatch error

    try another quicker approach with..
    Option Explicit
    
    Sub Web_Table_Option_One()
    Dim xml    As Object
    Dim html   As Object
    Dim objTable As Object
    Dim result As String
    Dim lRow As Long
    Dim lngTable As Long
    Dim lngRow As Long
    Dim lngCol As Long
    Dim ActRw As Long
    Set xml = CreateObject("MSXML2.XMLHTTP.6.0")
    With xml
    .Open "GET", "http://www.racingandsports.com.au/en/form-guide/", False
    .send
    End With
    result = xml.responseText
    Set html = CreateObject("htmlfile")
    html.body.innerHTML = result
    Set objTable = html.getElementsByTagName("table")
     For lngTable = 0 To objTable.Length - 1
            For lngRow = 0 To objTable(lngTable).Rows.Length - 1
                For lngCol = 0 To objTable(lngTable).Rows(lngRow).Cells.Length - 1
                    ThisWorkbook.Sheets("Sheet1").Cells(ActRw + lngRow + 1, lngCol + 1) = objTable(lngTable).Rows(lngRow).Cells(lngCol).innerText
                Next lngCol
            Next lngRow
            ActRw = ActRw + objTable(lngTable).Rows.Length + 1
        Next lngTable
    End Sub
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: getElementById run-time 13 type mismatch error

    Hello killerkoz17,

    When automating Internet Explorer, the application code runs asynchronously from the VBA code being executed. This is why you see delay loops included in the VBA code.

    To get Internet Explorer to sync up with VBA, you have to test key objects and events. The code below will do that correctly now.

    Sub getElementByIdIssue()
    
        Dim Doc As Object
        Dim CountryHTML As String
        Dim IE As InternetExplorer
    
            Set IE = New InternetExplorer
    
                IE.Visible = False
                IE.Navigate ("http://www.racingandsports.com.au/en/form-guide/")
    
              ' Wait for the Web page to finish loading and Excel to stop processing.
                While IE.Busy And IE.ReadyState <> 4: DoEvents: Wend
    
              ' Wait for the document to be converted from HTML to HTML code.
                While Doc Is Nothing: Set Doc = IE.document: Wend
     
              ' Yield to IE to finish handling it's events.
                DoEvents
                CountryHTML = Doc.getElementById("accordion36").innerHTML
    
            Range("A1") = CountryHTML 'added this line here just to test it's working
    
            IE.Quit
    
    '...(pull out various bits of data in the full code)
    
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Registered User
    Join Date
    02-19-2013
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2013
    Posts
    33

    Re: getElementById run-time 13 type mismatch error

    Thanks for the replies guys.

    Pike, thanks for the code, I'll keep that (still an XML beginner and that will help me learn). I guess I'm just trying to understand why this apparently simple code error is occurring.

    Leith, thanks for the response. I tried your code as it is written but I'm getting a run-time 424 Object required error that errors on the getElementById line. When I use your code within my full code I get run-time error 91 - Object variable or With block variable not set and an error on the same line.

    Cheers, Rob

  5. #5
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: getElementById run-time 13 type mismatch error

    Killer
    change false to
     IE.Visible = True
    or add a little time delay

        IE.Visible = False
                  IE.Navigate ("http://www.racingandsports.com.au/en/form-guide/")
                  Application.Wait (Now + TimeValue("0:00:03"))
    Last edited by pike; 12-30-2014 at 06:55 PM.

  6. #6
    Registered User
    Join Date
    02-19-2013
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2013
    Posts
    33

    Re: getElementById run-time 13 type mismatch error

    Thanks for your efforts pike and Leith. pike, your addition got Leith's code working on my computer but unfortunately no effect on the full code on my friend's computer. Same run-time 13 type mismatch error

  7. #7
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: getElementById run-time 13 type mismatch error

    try winding the timer us a little to
     Application.Wait (Now + TimeValue("0:00:15"))

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Run time error type mismatch 13
    By Dariusd7 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-15-2013, 05:11 PM
  2. run-time error '13' type mismatch
    By dp1 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-06-2012, 02:43 AM
  3. Run Time Error 13 Type Mismatch
    By nahge123 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-07-2012, 05:46 PM
  4. [SOLVED] Please help with Run-Time error 13 Type Mismatch
    By alec66317 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-14-2012, 08:29 AM
  5. Run-Time error '13': Type mismatch
    By Drew Watson in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-28-2010, 08:39 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1