VBA to Return Goole Results STOPPED WORKING...PLEASE HELP

mjl4477

New Member
Joined
Sep 6, 2015
Messages
12
I used the following code(See at the bottom of this post) to return google results. The code has worked for the last couple of days and now it suddenly started giving me an error.


Error 91 " Object Variable or With Block variable not set". The debugger refers to these two lines of code.
<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: inherit;">Set objH3 = objResultDiv.getelementsbytagname("H3")(0)
Set link = objH3.getelementsbytagname("a")(0)</code>





Full Code:


<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: inherit;">Sub XMLHTTP()

Dim url As String, lastRow As Long
Dim XMLHTTP As Object, html As Object, objResultDiv As Object, objH3 As Object, link As Object
Dim start_time As Date
Dim end_time As Date

lastRow
= Range("A" & Rows.Count).End(xlUp).Row

Dim cookie As String
Dim result_cookie As String

start_time
= Time
Debug
.Print "start_time:" & start_time

For i = 2 To lastRow

url
= "https://www.google.co.in/search?q=" & Cells(i, 1) & "&rnd=" & WorksheetFunction.RandBetween(1, 10000)

Set XMLHTTP = CreateObject("MSXML2.serverXMLHTTP")
XMLHTTP
.Open "GET", url, False
XMLHTTP
.setRequestHeader "Content-Type", "text/xml"
XMLHTTP
.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:25.0) Gecko/20100101 Firefox/25.0"
XMLHTTP
.send

Set html = CreateObject("htmlfile")
html
.body.innerHTML = XMLHTTP.ResponseText
Set objResultDiv = html.getelementbyid("rso")
Set objH3 = objResultDiv.getelementsbytagname("H3")(0)
Set link = objH3.getelementsbytagname("a")(0)


str_text
= Replace(link.innerHTML, "<EM>", "")
str_text
= Replace(str_text, "</EM>", "")

Cells
(i, 2) = str_text
Cells
(i, 3) = link.href
DoEvents
Next

end_time
= Time
Debug
.Print "end_time:" & end_time

Debug
.Print "done" & "Time taken : " & DateDiff("n", start_time, end_time)
MsgBox
"done" & "Time taken : " & DateDiff("n", start_time, end_time)
End Sub</code>
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
I diabled an add-in and it started working again. Now it stopped working. So frustrating.
 
Upvote 0
Also the code above works on other computers but not mine. Is this a security issue?

I think it might be. I just got the same thing, when I went to Google in Chrome browser and I had to answer a captcha as it had detected unusual traffic. My normal browsing resumed after answering that but the VBA still didn't work. I restarted my router to get a new IP address and the VBA ran again. There must have been a block on that specific request from Excel from my specific IP address.
 
Upvote 0

Forum statistics

Threads
1,214,864
Messages
6,121,984
Members
449,058
Latest member
oculus

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top