Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Reddit
Contact us
Hide Buttons

VBA-Excel: Perform Google Search on Internet Explorer using Microsoft Excel

This arti­cle will teach you about how you can per­form a google search on Inter­net Explorer using Microsoft Excel.

Steps:

  • Cre­ate the object of Shell Application
  • Get all the win­dows using shellobject.Windows
  • Nav­i­gate all the windows
  • check if win­dow is Inter­net Explorer.
  • if win­dow is IE then check if it is Google Search browser and store the object
  • Get the Page object of Google Search window.
  • Iden­tify the objects on the Page using “GetEle­ment­ById” and “getElementsByName”
  • Put the search text
  • Wait for 3–4 secs
  • Click the Search button


Cre­ate the object of Shell Application

Set objShell = CreateObject(“Shell.Application”)

Get all the win­dows using shellobject.Windows

Set objAll­Win­dows = objShell.Windows

Nav­i­gate all the windows

For Each ow In objAllWindows

check if win­dow is Inter­net Explorer

If (InStr(1, ow, “Inter­net Explorer”, vbTextCom­pare)) Then

if win­dow is IE then check if it is Google Search browser and store the object

If (InStr(1, ow.locationURL, “www.google.”, vbTextCom­pare)) Then

Set obj­Google = ow

End If

Get the Page object of Google Search window.

Set obj­Page = objGoogle.Document

Iden­tify the objects on the Page using “GetEle­ment­ById” and ” getElementsByName”

Set SearchEd­itB = objPage.getElementByID(“gbqfq”)

Set Search = objPage.getElementsByName(“btnG”)

Put the search text

SearchEditB.Value = “Excel macro Sumit Jain”

Wait for 3–4 secs

FnWait (5)

Click the Search button

Search(0).Click

Com­plete Code:

Sub googleSearch()

    Set objShell = CreateObject("Shell.Application")
    
    Set objAllWindows = objShell.Windows
    
    For Each ow In objAllWindows
    
        If (InStr(1, ow, "Internet Explorer", vbTextCompare)) Then
            If (InStr(1, ow.locationURL, "www.google.", vbTextCompare)) Then
                Set objGoogle = ow
            End If
        End If
        
    Next
    
    If objGoogle Is Nothing Then
    Else
        Set objPage = objGoogle.Document
        
        Set SearchEditB = objPage.getElementByID("gbqfq")
        
        SearchEditB.Value = "Excel macro Sumit Jain"
        
        FnWait (5)
        
        Set Search = objPage.getElementsByName("btnG")
        
        Search(0).Click
    End If

End Sub

Function FnWait(intTime)

    newHour = Hour(Now())

    newMinute = Minute(Now())

    newSecond = Second(Now()) + intTime

    waitTime = TimeSerial(newHour, newMinute, newSecond)

    Application.Wait waitTime

End Function

Put search Text

Put search Text

Searched Results

Searched Results

 

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: