html – How to do a google search with vba?

Question:

I'm having trouble putting together 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 look for the word "test" in the search.

Follow 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.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

Answer:

I was able to understand the problem.

here's 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
Scroll to Top