Be the first user to complete this post

  • 0
Add to List

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

This article will teach you about how you can perform a google search on Internet Explorer using Microsoft Excel.

Steps:

  • Create the object of Shell Application
  • Get all the windows using shellobject.Windows
  • Navigate all the windows
  • check if window is Internet Explorer.
  • if window 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 “GetElementById” and "getElementsByName"
  • Put the search text
  • Wait for 3-4 secs
  • Click the Search button


Create the object of Shell Application

Set objShell = CreateObject("Shell.Application")

Get all the windows using shellobject.Windows

Set objAllWindows = objShell.Windows

Navigate all the windows

For Each ow In objAllWindows

check if window is Internet Explorer

If (InStr(1, ow, "Internet Explorer", vbTextCompare)) Then

if window is IE then check if it is Google Search browser and store the object

If (InStr(1, ow.locationURL, "www.google.", vbTextCompare)) Then

Set objGoogle = ow

End If

Get the Page object of Google Search window.

Set objPage = objGoogle.Document

Iden­tify the objects on the Page using “GetElementById” and " getElementsByName"

Set SearchEditB = 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

Complete 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



Also Read:

  1. VBA-Excel: Open word document using GetObject()
  2. VBA-Excel: Read XML by Looping through Nodes
  3. Excel-VBA : Insert Multiple Images from a Folder to Excel Cells
  4. Excel-VBA : Open a MS Word Document using Excel File using Explorer Window.
  5. VBA-Excel: Create worksheets with Names in Specific Format/Pattern.