Home » excel » excel – Launch a Google search from VBA Macro on Mac

excel – Launch a Google search from VBA Macro on Mac

Posted by: admin April 23, 2020 Leave a comment


Just up front, I’m not a true programmer. I work in editing, and our company uses many macros to streamline our process and functionality (lots of findReplace, identifying inconsistent spelling/spacing conventions, etc.). That being said, I’ve begun to pick up some things on how the VBA editor works on Microsoft Word.

We have a Fetch macro (originally created by Paul Beverley) that works amazingly for all of the employees who use PC. It takes the highlighted term in the document, automatically opens a web browser, and performs the google search. However, we have several employees who use Office for Mac 2011, and they are unable to use the same functionality as the PC users. I’d really like to adjust the macro such that it will work on a Mac, but my lack of training is seriously hampering me.

Here is the original script that our company uses for PC:

Sub GoogleFetch()
' Version 08.05.12
' Launch selected text on Google

useExplorer = False

runBrowser = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"

mySite = "http://www.google.com/#q="

If Len(Selection) = 1 Then Selection.Words(1).Select
mySubject = Trim(Selection)
mySubject = Replace(mySubject, "&", "%26")
mySubject = Replace(mySubject, " ", "+")

If useExplorer = True Then
  Set objIE = CreateObject("InternetExplorer.application")
  objIE.Visible = True
  objIE.navigate mySite & mySubject
  Set objIE = Nothing
  Shell (runBrowser & " " & mySite & mySubject) 'ERROR HERE
End If
End Sub

(I’ll probably eliminate the useExplorer function.)

I’ve tried using Macscript and OpenURL to get around the runBrowser issue (the error pops up Shell ... line because I can’t get the path right), but it’s causing more of a headache.

Is there some simple fix that I am missing to get around this? Thanks in advance for any help!

How to&Answers:

You can treat the two systems (Mac and PC) differently with use of an #If ... #End If, note the # at the beginning. This will run at compile time and depending on the system, leave only the suitable code to be executed at run time.

You can use MacScript to open Safari quite easily as I found out here: http://www.officeonemac.com/vba/open_url.html

So your code would look something like this:

Sub GoogleFetch()
    ' Version 08.05.12
    ' Launch selected text on Google

    runBrowser = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
    mysite = "http://www.google.com/#q="

    If Len(Selection) = 1 Then Selection.Words(1).Select
    mysubject = Trim(Selection)
    mysubject = Replace(mysubject, "&", "%26")
    mysubject = Replace(mysubject, " ", "+")

    #If Mac Then
        OpenURL mysite & mysubject
        Shell runBrowser & " " & mysite & mysubject
    #End If
End Sub

Private Sub OpenURL(ByVal URL As String)
    Dim s As String
    s = "tell application ""Safari""" + vbCrLf + _
        "open location """ + URL + """" + vbCrLf + _
        "end tell"
    MacScript s
End Sub