Home » excel » excel – while compiling the following code i have an error Object variable or with block variable not set

excel – while compiling the following code i have an error Object variable or with block variable not set

Posted by: admin May 14, 2020 Leave a comment

Questions:
 Sub GetJobDetails()
 Const URL$ = "https://www.linkedin.com/jobs/search/?geoId=103644278&keywords=nav&location=United%20States&start="
 Dim IE As Object: Set IE = CreateObject("InternetExplorer.Application")
 Dim post As Object, elem$, R&, I&: I = 0
 Do
    With IE
        .Visible = True
        .navigate URL & I
        While .Busy Or .readyState < 4: DoEvents: Wend

        On Error Resume Next
        elem = .document.getElementsByTagName("li")(0).innerText
        On Error GoTo 0

        If elem = "" Then Exit Do

        For Each post In .document.getElementsByTagName("li")
            R = R + 1: Cells(R, 1) = post.getElementsByClassName("js_focusable")(0).innerText          
Cells(R, 2) = post.getElementsByClassName("job-card-search__company-name-link")(0).innerText                   
    Cells(R, 3) = post.getElementsByClassName("job-card-search__location")(0).innerText
    Next post
    End With
    I = I + 25
    elem = ""
    Application.Wait Now + TimeValue("00:00:05")
    Loop
    IE.Quit
 End Sub

what exactly

“Object variable or with block variable not set”

means,may be the url was wrong,The url will changes dynamically like in last
“start =25/50/75”

How to&Answers:

To get the content from next pages you need to log in to that site first. The good news is you can parse the content from it’s landing page using xhr which is way faster than IE.

Working script (content from first page only):

Sub GetJobDetails()
    Const URL$ = "https://www.linkedin.com/jobs/search/?geoId=103644278&keywords=nav&location=United%20States&start=0&redirect=false&position=1&pageNum=0"
    Dim Html As New HTMLDocument, Htmldoc As New HTMLDocument
    Dim I&, R&

    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", URL, False
        .setRequestHeader "User-Agent", "Mozilla/5.0"
        .send
        Html.body.innerHTML = .responseText

        With Html.querySelectorAll("li.job-result-card")
            For I = 0 To .Length - 1
                Htmldoc.body.innerHTML = .Item(I).outerHTML
                R = R + 1: Cells(R, 1) = Htmldoc.querySelector(".screen-reader-text").innerText
                On Error Resume Next
                Cells(R, 2) = Htmldoc.querySelector("h4.result-card__subtitle > a").innerText
                On Error GoTo 0
                Cells(R, 3) = Htmldoc.querySelector("span.job-result-card__location").innerText
            Next I
        End With
    End With
End Sub

Before executing the above script make sure to add the following reference:

Microsoft HTML Object Library

Once again, you need to log in to that site to parse the content from next pages.