0

I followed a Tutorial and have an error while executing the code i copied:

Private Sub CommandButton1_Click()

    Dim internet As Object
    Dim internetdata As Object
    Dim div_result As Object
    Dim header_links As Object
    Dim link As Object
    Dim URL As String

    Set internet = CreateObject("InternetExplorer.Application")
    internet.Visible = True

    URL = "https://www.google.co.in/search?q=how+to+program+in+vba"
    internet.navigate URL

    Do Until internet.readyState >= 4
        DoEvents
    Loop

    Application.Wait Now + TimeSerial(0, 0, 5)

    Set internetdata = internet.document
    Set div_result = internetdata.getElementById("res")


    Set header_links = div_result.getElementsByTagName("h3")

    For Each h In header_links
        Set link = h.ChildNodes.Item(0)
        Cells(Range("A" & Rows.Count).End(xlUp).Row + 1, 1) = link.href
    Next

    MsgBox "done"

End Sub

The error comes at

Cells(Range("A" & Rows.Count).End(xlUp).Row + 1, 1) = link.href

What ist wrong here?

EDIT: The code should give the URLs out of a google search and write it to excel list:

The code is copied from here:

Getting Links/URL from a webpage-Excel VBA

  • 2
    The error message tells you that probably `link` is `Nothing` or not a link object and therefore has no `.href`. – Pᴇʜ May 15 '19 at 13:56
  • 2
    @JNevill `Set link = h.ChildNodes.Item(0)` - it took me a while to find it lol. – BigBen May 15 '19 at 13:57
  • Thank you for the answer! ...The code is 1:1 copied from a tutorial so it should work? ... how can i determine if link is nothing? Or how to try/catch the error if it is nothing? – user3659746 May 15 '19 at 13:58
  • Actually this is an [XY-Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem): You ask X but the problem is Y. • Please [edit] your question (don't use comments) and add more information. What is the goal of your code? What would be the expected outcome? • Also include the link of the tutorial please. – Pᴇʜ May 15 '19 at 14:02
  • @PEH: Edited the question! Thx – user3659746 May 15 '19 at 14:10

2 Answers2

0

The issue is that Google obviously changed the code of the website since the tutorial was written. This method is not very reliable as everytime Google changes the website it can easily break your code.

Try the following

Set div_result = internetdata.getElementById("res")
Set header_links = div_result.getElementsByTagName("a")

Dim h As Variant
For Each h In header_links
    Cells(Range("A" & Rows.Count).End(xlUp).Row + 1, 1) = h.href
Next
Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
0

If you want just the web results without the featured content and video links, you can use below. Also in this code you can control the page number you want to get the links fromby changing the variable pageNo.

Private Sub CommandButton1_Click()

    Dim internet As Object
    Dim internetdata As Object
    Dim div_result As Object
    Dim header_links As Object
    Dim link As Object
    Dim URL As String
    Const pageNo = 10  '0 is page 1, 10 is page 2 and so on  0;10;20;30;40

    Set internet = CreateObject("InternetExplorer.Application")
    internet.Visible = True

    URL = "https://www.google.co.in/search?q=how+to+program+in+vba"
    internet.navigate URL & "&start=" & pageNo

    Do Until internet.readyState >= 4
        DoEvents
    Loop

    Application.Wait Now + TimeSerial(0, 0, 5)

    Set internetdata = internet.document
    Set div_result = internetdata.getElementById("res")
    Set header_links = div_result.getelementsbytagname("h2")

    Dim h As Variant
    For Each div In header_links
        If div.innertext = "Web results" Then
            Set Links = div.ParentElement.getelementsbytagname("a")
            For Each link In Links
                Cells(Range("A" & Rows.Count).End(xlUp).Row + 1, 1) = link.href
            Next
        End If
    Next
    MsgBox "done"

End Sub

Jitendra Singh
  • 191
  • 1
  • 8