gpt4 book ai didi

excel - 在 Mac 上从 VBA 宏启动 Google 搜索

转载 作者:行者123 更新时间:2023-12-02 19:21:23 25 4
gpt4 key购买 nike

首先,我不是一个真正的程序员。我从事编辑工作,我们公司使用许多宏来简化我们的流程和功能(大量 findReplace、识别不一致的拼写/空格约定等)。话虽这么说,我已经开始了解 VBA 编辑器如何在 Microsoft Word 上工作的一些知识。

我们有一个 Fetch 宏(最初由 Paul Beverley 创建),它对所有使用 PC 的员工来说都非常有效。它会获取文档中突出显示的术语,自动打开网络浏览器,并执行谷歌搜索。然而,我们有几名员工使用Office for Mac 2011,他们无法使用与PC用户相同的功能。我真的很想调整宏,使其可以在 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 问题(错误会弹出 Shell ... 行,因为我无法获得正确的路径),但它导致了更多头疼。

是否有一些我缺少的简单修复来解决这个问题?预先感谢您的帮助!

最佳答案

您可以使用 #If ... #End If 来区别对待两个系统(Mac 和 PC),请注意开头的 #。这将在编译时运行,并根据系统的不同,只留下合适的代码在运行时执行。

您可以使用 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

关于excel - 在 Mac 上从 VBA 宏启动 Google 搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42652648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com