+ Reply to Thread
Results 1 to 24 of 24

Automate Google Search via Excel

Hybrid View

  1. #1
    Registered User
    Join Date
    11-03-2008
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    2,521

    Automate Google Search via Excel

    Dear Forum,

    I am in need of expediting a task which I have to do almost on a daily basis, I am an analyst and have to research on different topics depending on the nature of each project..

    Presently, I am working on a project dealing with some chemicals and checking for different Buyers and Suppliers..

    I get the names of these Chemicals in an plain Excel Sheet one below the other and I have to research on each chemical and its different buyers and suppliers ( i.e. Dealers)

    Now I have to everytime go in the Excel Sheet and then open a New Window - GOOGLE to perform the same...

    I would appreciate if there could be some easier way if possible through VBA which is my perennial weakness..

    Please find the attached file for your perusal, I have just put 10 names however I get about 35-45 names on a daily basis and therefore its very tedious..
    Attached Files Attached Files
    Last edited by e4excel; 07-22-2010 at 11:51 AM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Automating Google Search through an Excel File!

    Hello e4excel,

    I have added two macros to your workbook to automate the process. Now whenever a cell in column "C" changes to "Search", the contents of cell "B" in the same row are used as the Google search term.

    The second macro starts Internet Explorer, opens to Google's search page, fills it in with data from the cell, and clicks the search button. Each search is opened in the same window. You can then use the back and forward buttons to review your searches.

    Sheet2 Worksheet_Change() Macro Code
    Private Sub Worksheet_Change(ByVal Target As Range)
    
        If Target.Cells.Count > 1 Then Exit Sub
        
        If Not Intersect(Target, Range("C2:C" & Rows.Count)) Is Nothing Then
           ThisWorkbook.GoogleSearch Target.Offset(0, -1).Text
        End If
        
    End Sub

    ThisWorkbook Module Code
    'Written: July 21, 2010
    'Author:  Leith Ross
    'Summary: Opens Internet Explorer to Google's search page. It then automatically inserts
    '         the search term and clicks the search button. New searchs are opened in the
    '         same window.
    
    Public ieApp As Object
    
    Sub GoogleSearch(ByVal SearchTerm As String)
    
      Dim ieDoc As Object
      Dim InputBox As Object
      Dim SearchButton As Object
       
        If TypeName(ieApp) = "Object" Or TypeName(ieApp) = "Nothing" Then
            Set ieApp = CreateObject("InternetExplorer.Application")
            ieApp.Navigate2 "www.google.com", 2048
        End If
      
          While ieApp.ReadyState <> 4
            DoEvents
          Wend
          
          ieApp.Visible = True
          
          Set ieDoc = ieApp.Document
        
            Set InputBox = ieDoc.getElementsByName("q")
            Set SearchButton = ieDoc.getElementsByName("btnG")
            
            InputBox.Item(0).Value = SearchTerm
            SearchButton.Item(0).Click
            
          Set ieDoc = Nothing
          
    End Sub
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    11-03-2008
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    2,521

    Re: Automating Google Search through an Excel File!

    Dear Leith,

    I liked the concept however nothings happenning, I dont know why..please check I have done the Enable Macros after opening the file.

    But nothing seemed to work after I use the drop-dow search...

    Please let me know if I am missing anything!

    Warm Regards
    e4excel

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Automating Google Search through an Excel File!

    Hello e4excel,

    Can you post a copy of your file in Excel 2003 format?

  5. #5
    Registered User
    Join Date
    11-03-2008
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    2,521

    Re: Automating Google Search through an Excel File!

    Hi Leith,

    Please find the attached file..I think the earlier one too was in 97-2003 however check this out..!

    Thanks for helping..
    Attached Files Attached Files

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Automating Google Search through an Excel File!

    Hello e4excel,

    There were no macros in the workbook. I have added them back in. Be sure to enable macros in Excel 2007. If you don't nothing will happen. Click here if you aren't sure how do this.
    Attached Files Attached Files

  7. #7
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Automating Google Search through an Excel File!

    Maybe this might help
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  8. #8
    Registered User
    Join Date
    11-03-2008
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    2,521

    Re: Automating Google Search through an Excel File!

    Dear Roy,

    Yes maybe this is useful too howver I was looking for a personalized one as at work add-ins are a strict no-no..and this customised utility would serve the purpose without any breach..

    Wish i could use this utility which leith had made!

  9. #9
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Automating Google Search through an Excel File!

    Does this work?
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    11-03-2008
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    2,521

    Re: Automating Google Search through an Excel File!

    Dear Gurus,

    Thanks a lot RoyUk, it does work and I am sure Leith's file would work too , however I am not able to check the same due to IE 8 presence..

    But I have added to both your reputations thanks alot, this is indeed going to save a lot of time!

    P.S. RoyUk I hope this file would work irrespective of the Internet Explorer!

  11. #11
    Registered User
    Join Date
    11-03-2008
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    2,521

    Re: Automating Google Search through an Excel File!

    Dear RoyUk,

    I am extremely happy to have got his working, however there's only one more thing if i can ask is..

    By default we use GOOGLE for everything but there are times when we also use another site called www.alibaba.com..

    I took the liberty to change that in your code it did open the file but could not paste the Search Keyword and also I got an error!!!!

    What I did is to temporarily comment the below:

    Option Explicit
    
    
    
    Can I add a Different Search Like Google and also Alibaba both in the dropdown..so that I can use both their functionalities
    
    Sub GetResults(r As Range)
        Dim IE As Object
        Dim frm As Object
        Dim srch As Object
        Set IE = CreateObject("InternetExplorer.Application")
        IE.Visible = True
       ' IE.Navigate "http://www.google.com"
        IE.Navigate "http://www.alibaba.com" ' New Line
    
        Do While IE.Busy: DoEvents: Loop
        Do While IE.ReadyState <> 4: DoEvents: Loop
        Set frm = IE.Document.forms("f")
        Set srch = frm.Document.all("q")
        srch.Value = r.Value
        frm.submit
    End Sub

    However, I am getting an Error 91:

    Object variable or With block Variable not Set


    Can I have both the Search Sites in use as both are useful to me! by putting them in the Drop-down as below:

    GOOGLE Search
    Alibaba Search

    instead of just Search...?
    Last edited by e4excel; 07-22-2010 at 10:08 AM.

  12. #12
    Registered User
    Join Date
    11-03-2008
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    2,521

    Re: Automating Google Search through an Excel File!

    Dear RoyUk,

    Sorry to bother again , however for faster search sometimes we also use like this :

    i.e. Keyword + GOOGLE + ALI BABA + MANUFACTURERS

    Now the keyword is any value or chemical name in this file appended with the SEarch Engines + An additional Criterion

    Warm regards
    e4excel

  13. #13
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Automate Google Search via Excel

    Hello e4excel,

    I did some more reading on the Explorer code and I believe that this code will work through Internet Explorer 7. I added to your drop down list. You can search by Google or Alibaba. The macro will open a new tab in the same window. The drop down also includes "Suppliers" and "Manufacturers". Even if this code doesn't run, it will still be a good template to draw from.

    Sheet2 Worksheet_Change() Event Code
    Private Sub Worksheet_Change(ByVal Target As Range)
    
      Dim I As Long
      Dim SearchTerms As String
      
        If Target.Cells.Count > 1 Then Exit Sub
        
        If Not Intersect(Target, Range("C2:C" & Rows.Count)) Is Nothing Then
           SearchTerms = Target.Offset(0, -1).Text
           I = InStr(1, Target, "+")
           If I Then SearchTerms = SearchTerms & " " & Right(Target, Len(Target) - I)
           
           Select Case Target.Text
             Case Is = "Search Google", "Search Google + Suppliers", "Search Google + Manufacturers"
               InternetSearch SearchTerms, "www.google.com"
             Case Is = "Search Alibaba", "Search Alibaba + Suppliers", "Search Alibaba + Manufacturers"
               InternetSearch SearchTerms, "www.alibaba.com"
           End Select
        End If
        
    End Sub

    Module Macro Code
    Public ieApp As Object
    
    Sub InternetSearch(ByVal SearchTerm As String, ByVal SearchEngine As String)
    
      Dim ieDoc As Object
      Dim InputBox As Object
      Dim SearchButton As Object
      Dim URL As String
       
        
        If TypeName(ieApp) = "Object" Or TypeName(ieApp) = "Nothing" Then
           Set ieApp = CreateObject("InternetExplorer.Application")
           ieApp.Visible = True
           GoSub OpenNewTab
        End If
          
          Set ieDoc = ieApp.Document
          
          If ieDoc.Domain <> "" And ieDoc.Domain <> SearchEngine Then GoSub OpenNewTab
          
            Select Case ieDoc.Domain
              Case Is = "www.google.com"
                Set InputBox = ieDoc.getElementsByName("q")
                Set SearchButton = ieDoc.getElementsByName("btnG")
                  InputBox.Item(0).Value = SearchTerm
                  SearchButton.Item(0).Click
              Case Is = "alibaba.com"
                Set InputBox = ieDoc.getElementById("SearchTextIdx")
                Set SearchButton = ieDoc.getElementById("searchSubmit")
                  InputBox.Value = SearchTerm
                  SearchButton.Click
            End Select
            
          Set ieDoc = Nothing
          
    Exit Sub
    
    OpenNewTab:
           While ieApp.Busy
             DoEvents
           Wend
           
           ieApp.Navigate SearchEngine, 2048
           
           While ieApp.Busy
             DoEvents
           Wend
    Return
    
    End Sub
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1