How to do a google search with vba?

3

I'm having trouble putting together a VBA program that does a google search. I can not 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.

Follow the code below:

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.br/?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
    
asked by anonymous 07.07.2015 / 18:38

2 answers

1

I was able to understand the problem.

Here is the 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.br/?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
    
07.07.2015 / 19:05
0

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

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

Again, I think this way of launching the google search is contraindicated. I would run a Shell with the googgle address, inserting the search text.

    
07.07.2015 / 18:59