0

I'm trying to use (2007) Excel VBA to open Internet Explorer, navigate to google.com, enter the text string from an Excel spreadsheet into the google.com search form, and then click the "Google Search" button.

The code I'm using opens Internet Explorer, navigates to google.com, and nicely inputs the Excel text string into the Google search form. But I can't seem to get my code to click the "Google Search" button. Any suggestions?

Here's the code I've got:

Sub FillInternetForm()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.navigate "http://www.google.com"
IE.Visible = True
While IE.Busy
DoEvents
Wend
IE.document.all("q").Value = ThisWorkbook.Sheets("Sheet1").Range("A1")
IE.document.all("btnK").Click
End Sub
3
  • 1
    Does this answer your question? How to use VBA to input text into Google and click search Commented Feb 16, 2021 at 13:55
  • Changing line 10 from IE.document.all("btnK").Click to SendKeys "{ENTER}" works, in this instance. (So, thank you for that.) But I'd like to get the code working by specifying the specific button (just in case pressing the "Enter" key doesn't always work. Commented Feb 16, 2021 at 14:38
  • 1
    Best practice is to avoid creating unnecessary dependency on low-level page designs that you can't control. Goog can change the button name/id anytime. The ENTER key performs Click() on whichever button of the page is marked default, so is "safer", but again creates dependency on which button is default (if any). Why not put the query into the URL as other answers have suggested? This seems safest to me. Commented Aug 17, 2023 at 12:06

2 Answers 2

2

If you don't feel an absolute need to click the search button, you can also just change the URL to contain the search parameter. In that case, navigating to the URL 'performs' the search without the need to press a button. See this link for details: https://webapps.stackexchange.com/questions/39818/how-should-i-write-the-url-for-a-specific-google-search-query

Sign up to request clarification or add additional context in comments.

1 Comment

I like that idea; it's a nice workaround. In this instance though, I think I'd like learn how to click the search button.
1

Following is a short Excel VBA macro that will run a Google search in your DEFAULT Web browser.

In its notes (inactive comment lines), it tells how to run the request in "Internet Explorer" or its successor, "Microsoft Edge."

I run it as written, to automatically open and work with my DEFAULT Web browser, "Firefox." There's no need to specify the default browser in the code.

The macro simply asks for your search term and then sets up and runs the Google search directly.

Here's the code:

Sub GoogleSearchFromVBA()

   Dim iReply As String

    iReply = InputBox(Prompt:="Enter key word or phrase")

    iReply = "https://www.google.com/search?q=" & iReply

Since Internet Explorer has been discontinued in favor of Edge, you can use the following VBA code to enter and run your Google search in Edge using the following code line:

CreateObject("Shell.Application").ShellExecute "microsoft-edge:" & iReply

If you still have Internet Explorer installed, use this to run the search:

CreateObject("Shell.Application").ShellExecute "internetexplorer:" & iReply

However, if you'd like to simply use your default browser, erase or inactivate (via the apostrophe) the createObject Line and remove the apostrophy to activate the following:

'Otherwise, this will run the search on your default 'web browser, whatever it is. That's the best approach:

ActiveWorkbook.FollowHyperlink Address:=iReply


End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.