在Mac上从VBAmacros启动Googlesearch

就在前面,我不是一个真正的程序员。 我在编辑工作,我们公司使用许多macros来简化我们的stream程和function(大量的findReplace,识别不一致的拼写/间距约定等)。 话虽如此,我已经开始拿起一些关于VBA编辑器如何在Microsoft Word上工作的东西。

我们有一个Fetchmacros(最初由Paul Beverley创build),对于所有使用PC的员工来说都非常有效。 它需要在文档中突出显示的术语,自动打开网页浏览器,并执行谷歌search。 但是,我们有几个使用Office 2011 for Mac的员工,他们无法使用与PC用户相同的function。 我真的很想调整macros,这样它就可以在Mac上工作,但是我缺乏训练严重阻碍了我。

以下是我们公司用于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 

(我可能会删除useExplorer函数。)

我试过使用Macscript和OpenURL来解决runBrowser问题(这个错误popup了Shell ... line,因为我无法获得正确的path),但是这导致了更多的头痛。

是否有一些简单的修复,我错过了解决这个问题? 提前感谢任何帮助!

您可以使用#If ... #End If区别对待两个系统(Mac和PC) #If ... #End If在开头注意#号。 这将在编译时运行,并且取决于系统,只保留合适的代码在运行时执行。

您可以使用MacScript轻松打开Safari,如下所示: http : //www.officeonemac.com/vba/open_url.html

所以你的代码看起来像这样:

 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