Launch a Google search from VBA Macro on Mac

最后都变了- 提交于 2019-12-23 17:27:06

问题


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
Else
  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!


回答1:


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
    #Else
        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


来源:https://stackoverflow.com/questions/42652648/launch-a-google-search-from-vba-macro-on-mac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!