Windows和64位Mac上的打开URL

时间:2019-02-23 19:34:27

标签: excel vba macos url shellexecute

以下代码在Windows计算机上打开一个URL(Excel 2016、2013、2010)。

我也试图使其在64位Mac(Excel for Mac v.22.22,Office 365安装)上可用。

我尝试了多次迭代来查找Mac库“ libc.dylib”,通常会收到“运行时错误'53'。找不到文件'libc.dylib'”错误。一旦出现错误“运行时错误'453'。找不到文件'/usr/lib/libc.dylib'”。

以下是产生453错误的代码:

Option Explicit

Enum W32_Window_State
    Show_Normal = 1
    Show_Minimized = 2
    Show_Maximized = 3
    Show_Min_No_Active = 7
    Show_Default = 10
End Enum

#If Mac Then
    #If VBA7 Then
        Private Declare PtrSafe Function system Lib "/usr/lib/libc.dylib" (ByVal command As String) As LongPtr
    #Else
        Private Declare Function system Lib "/usr/lib/libc.dylib" (ByVal command As String) As Long
    #End If
#Else
    #If VBA7 Then
        Private Declare PtrSafe Function ShellExecute Lib "shell32.dll" _
            Alias "ShellExecuteA" (ByVal hwnd As LongPtr, _
            ByVal lpOperation As String, ByVal lpFile As String, _
            ByVal lpParameters As String, ByVal lpDirectory As String, _
            ByVal nShowCmd As Long) As LongPtr
    #Else
        Private Declare Function ShellExecute Lib "shell32.dll" _
            Alias "ShellExecuteA" (ByVal hwnd As Long, _
            ByVal lpOperation As String, ByVal lpFile As String, _
            ByVal lpParameters As String, ByVal lpDirectory As String, _
            ByVal nShowCmd As Long) As Long
    #End If
#End If

Public Function OpenURL(URL As String, WindowState As W32_Window_State) As Boolean

    'Opens passed URL with default application, or Error Code (<32) upon error

    #If VBA7 Then
        Dim lngHWnd As LongPtr
        Dim lngReturn As LongPtr
    #Else
        Dim lngHWnd As Long
        Dim lngReturn As Long
    #End If

    #If Mac Then
        lngReturn = system("open -a Safari --args " & URL)
    #Else
        lngReturn = ShellExecute(lngHWnd, "open", URL, vbNullString, _
            vbNullString, WindowState)
    #End If

    OpenURL = (lngReturn > 32)  ' With Mac, this may return a dummy variable, but we're going to do it anyways.

End Function

除了此处的代码外,我还尝试在文件路径中使用冒号代替斜杠。那也给了我一个错误“ 53,找不到文件”。

0 个答案:

没有答案