Need Help on WEB Automation - getting INNERTEXT from GOOGLESEARCH

VLAD_BASHAROV

New Member
Joined
Jun 26, 2022
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hello.
I have a list of Companies and I need do find their type of business. I decided to scrap this info from Google Search using getElementsByClassName. So, My VBA-Code is supposed to google every Company from list, then insert in next cell Innertext. But it doesn't work - every time i see a Run-time Error '91': Object variable or with block variable not set.

Help please - Could someone please have a look.

Thanks

Below - the List of Companies

TET-A-TET
CONOCO
NETCOST
MARSHALLS
LUXDEN BROOKLYN
MILLHOUSE LOGIST
WAL-MART



VBA Code:
Private Sub H_CLICK()
 Dim QUERY As String
 QUERY = ActiveCell
 Dim website As String
 Dim search_string As String
 Dim ie As Object
 Dim name As String
 Do Until IsEmpty(ActiveCell)
 ActiveCell.Offset(0, 1).Value = "RUNNING"
 search_string = Replace(QUERY, " ", "+")
 website = "https://www.google.com/search?hl=en&ie=UTF-8&q=" & search_string
 Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = 0
.navigate website
While .Busy Or .readyState <> 4
DoEvents
Wend
End With
Dim Doc As HTMLDocument
Set Doc = ie.document
ActiveCell.Offset(0, 1).Value = "ERROR"
name = Trim(Doc.getElementsByClassName("YhemCb")(2).innerText)
ActiveCell.Offset(0, 1).Value = name
ie.Quit
Loop
End Sub

Screenshot 2022-06-26 232609 (2).jpg
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
You didn't indicate on what line the error occurs. Assuming its the one with getElementsByClassName, the error message you're getting means that VBA can't find the class name.

Your screen capture indicates that you're looking at the code in Chrome, but that might not be helping. It is always best to evaluate the code in the browser that you intend to scrape with - here, you're using Internet Explorer. The reason for this is because the version of a web page may/will differ depending on the browser you're using, or more accurately(?), on the User Agent details that your browser has sent to the web server - this relates to how both how the page will appear and also the actual code that the server provides your browser.

As for your code, I would suggest that:
(1) You make Internet Explorer visible while you're trying to debug the code and work out what's going on. You can do this by changing the relevant line of your code to:
VBA Code:
.Visible = True
(2) Move the ie.Quit line out of the loop. Once you've quit it at the end of the first loop, the code will fail to work.... because you've just closed down the Internet Explorer you were using... So change the final few lines to:
VBA Code:
Loop
ie.Quit
End Sub

Hope that helps, and let us know how it goes.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,190
Members
452,616
Latest member
intern444

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