1

I've created a script to parse recipe name and serving from such links. When I run the same script in two different computers, I get expected results in one computer but I encounter this error Run-time error 438 (Object doesn't support this property or method) in another computer. The script throws that error pointing at this line Servings = .Item(I).NextSibling.innerText within the script below.

The excel version, OS and bit Excel 2019, windows 10, 64bit of that pc which errors out.

The excel version, OS and bit Excel 2013, windows 7, 32bit of that pc which finds success.

This is the script that I used :

Public Sub FetchRecipeInfo()
    Dim Http As Object, Html As HTMLDocument, Servings$, R&
    Dim Url As Variant, linkList As Variant, I&, Ws As Worksheet
    
    Set Html = New HTMLDocument
    Set Http = CreateObject("MSXML2.XMLHTTP")
    Set Ws = ThisWorkbook.Worksheets("Sheet1")
    R = 1

    linkList = Array( _
        "https://www.allrecipes.com/recipe/18871/easy-tuna-casserole/", _
        "https://www.allrecipes.com/recipe/21791/tuna-noodle-casserole-iv/" _
       )

    For Each Url In linkList
        With Http
            .Open "GET", Url, False
            .setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36"
            .send
            Html.body.innerHTML = .responseText
        End With
        
        Ws.Cells(R, 1) = Html.querySelector("h1.headline").innerText

        With Html.querySelectorAll(".recipe-meta-item-header")
            For I = 0 To .Length - 1
                If InStr(.item(I).innerText, "Servings:") > 0 Then
                    Servings = .item(I).NextSibling.innerText
                    Ws.Cells(R, 2) = Servings
                    Exit For
                End If
            Next I
        End With
        R = R + 1
    Next Url
End Sub

Expected output:

Easy Tuna Casserole 8

Why I get results in one pc but encounter Run-time error 438 in another pc even when I use the same script?

SMTH
  • 67
  • 1
  • 4
  • 17
  • Maybe another PC does not have Mozilla version update, therefore your xml parse is in different format or empty? – Kin Siang May 20 '21 at 03:18

1 Answers1

1

I don't know why it works on another machine. I assume something to do with the underlying parsers. With my set-up (Microsoft® Excel® 2019 MSO (16.0.13929.20206) 32-bit Windows 10 MSHTML.dll 11.00.19041.985), there is a text node as immediate adjacent sibling which doesn't have a .innerText property, but rather you could use .nodeValue. Instead, I think you wanted to move on to the next sibling i.e.

Servings = .Item(I).NextSibling.NextSibling.innerText

That works on Windows 10 64 bit. So, if this new line fails on your Win 7 then wrap in an If Else based on version.

QHarr
  • 83,427
  • 12
  • 54
  • 101
  • It works `.item(I).NextSibling.innerText` in excel 2013 but fails when I try this `.Item(I).NextSibling.NextSibling.innerText`. – SMTH May 20 '21 at 05:31
  • Wrap in an If Else I guess based on version. What is different, it seems, it the presence/absence of the intermediate text node / it's handling within the parser. – QHarr May 20 '21 at 05:32
  • Maybe related: https://stackoverflow.com/questions/1359469/innertext-works-in-ie-but-not-in-firefox – QHarr May 20 '21 at 05:59
  • Try printing out item(n).outerHTML on both machines and compare. – QHarr May 20 '21 at 06:03
  • This is `
    8
    ` what I get in `Excel 2013` . Unfortunately, I'm not around that pc in which `Excel 2019` is installed, so I could not test the other one.
    – SMTH May 20 '21 at 06:32
  • If I'm not wrong it's happening just because of the excel version, not because of the OS version. – SMTH May 20 '21 at 07:20
  • yes it runs except, I have to change from Object type in Set ingrContainer – QHarr May 20 '21 at 07:52
  • I also meant the parent div of those two divs – QHarr May 20 '21 at 07:52
  • What should I set the object type of `ingrContainer` to make it work in 2019? – SMTH May 20 '21 at 08:19
  • I had to `Dim As MSHTML.IHTMLDOMChildrenCollection` ;any variable that holds a nodelist returned by querySelectorAll. You have another var like this. – QHarr May 20 '21 at 08:27
  • So defining that object type `MSHTML.IHTMLDOMChildrenCollection` for `ingrContainer` worked for you in 2019, right? I'm deeply sorry to ask you too many questions. I wanted to install that office version in my windows but win 7 doesn't support that. – SMTH May 20 '21 at 08:36
  • 1
    Thanks a trillion for your great support. This thing `MSHTML.IHTMLDOMChildrenCollection` was totally foreign to me until you created your own post on it.. – SMTH May 20 '21 at 10:55