How to do a google search with vba?

Asked

Viewed 2,498 times

3

I’m having difficulty setting up a VBA programming that does a google search. I can’t understand the HTML part.

The idea is to identify the open internet explorer, go to the google page and search for the word "test" in the search.

Follows code:

Dim ie As SHDocVw.InternetExplorer
Dim objShell As Object
Dim objWindow As Object
Dim objItem As Object
Dim objCollection As Object

Sub teste()

Set objShell = CreateObject("Shell.Application")
Set objWindow = objShell.Windows()
For Each objItem In objWindow
    If LCase(objItem.FullName Like "*Internet Explorer*") Then
        Set ie = objItem
    End If
Next objItem

ie.navigate ("https://www.google.com/?gfe_rd=cr&ei=-s-bVf3ZBcf5gASo1oHAAw&gws_rd=ssl")

Do While ie.Busy Or ie.readyState <> 4
DoEvents
Loop '** Wait til page loaded

Set objCollection = ie.document.getElementsByTagName("input")

i = 0



Do While i < objCollection.all.Length
    If objCollection.all(i).ID = "q" Then
        objCollection.all(i).Value = "teste"
        Exit Do
    End If
    i = i + 1
Loop


End Sub
  • I think accessing internet explorer like this is risky and inefficient. In addition, accessing the content of the page to perform the search seems too complicated, unnecessarily. Why not use Shell to open the search address https://www.google.com/search?q={busca}? Windows will automatically open the default browser window, and a new tab if the browser is open.

  • What I wanted was to understand the logic of HTML, to use on other sites later.

  • Minto, I think doing this will open windows explorer, regardless of which is the default browser. Still recommend this line.

  • What do you mean by html logic? You want to analyze google page html?

  • Yes, I am not able to search the fields by html. It would be the same logic to fill in online forms, parse the HTML to identify the field id and use the VBA to fill it.

2 answers

1

I could understand the problem.

following solution I found:

Dim ie As SHDocVw.InternetExplorer
Dim objShell As Object
Dim objWindow As Object
Dim objItem As Object
Dim objCollection As Object

Sub teste()

Set objShell = CreateObject("Shell.Application")
Set objWindow = objShell.Windows()
For Each objItem In objWindow
    If LCase(objItem.FullName Like "*Internet Explorer*") Then
        Set ie = objItem
    End If
Next objItem

ie.navigate ("https://www.google.com/?gfe_rd=cr&ei=-s-bVf3ZBcf5gASo1oHAAw&gws_rd=ssl")

Do While ie.Busy Or ie.READYSTATE <> 4
DoEvents
Loop '** Wait til page loaded

Set objCollection = ie.document.getElementsByTagName("input")

i = 0



Do While i < objCollection.Length
    If objCollection(i).Name = "q" Then
        objCollection(i).Value = "teste"
        Exit Do
    End If
    i = i + 1
Loop


End Sub

0

On the google page, the ID is not "q".

The element input where the search has attribute name="q".

Again, I think this way to launch the search on google is contraindicated. I would run a Shell with the googgle address, inserting the searchable text.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.