如何使用Excel Mac VBA打开非Excel文件



ExcelWINDOWS有许多问答,但没有一个专门回答这个问题。

使用 Excel Mac 2011 VBA,如何使用文件的默认应用程序打开非 Excel 文件?

经过大量的研究和测试,我想出了答案。 见下文。

这适用于在macOS 10.12.6上运行Microsoft Excel 14.7.2(14.7.2(。

'''vb

Sub open_file()
Dim scriptStr As String
Dim hfsPath As String
hfsPath = "~:Documents:Test File To Open From Excel.txt"
'--- Create AppleScript to Open Non-Excel File ---
'       (Note: You cannot use POSIX path or POSIX commands)
'          So, I have allowed for the tilde in a HFS path
'         to mean the same as in a POSIX path:  Users Home Folder
scriptStr = "set hfsPath to """ & hfsPath & """" & vbNewLine & _
"if (hfsPath starts with ""~"") then" & vbNewLine & _
"   set homePath to (path to home folder) as text" & vbNewLine & _
"   set hfsPath to homePath & (text 3 thru -1 of hfsPath)" & vbNewLine & _
"end if" & vbNewLine & _
"tell application ""Finder"" to open file hfsPath" & vbNewLine & _
"return hfsPath"
Debug.Print scriptStr
'--- Execute AppleScript to Open Non-Excel File ---
hfsPath = MacScript(scriptStr)
Debug.Print hfsPath
End Sub

'''

对于 Excel Windows,请参阅
如何使用默认应用程序打开 Excel Windows VBA 文件

最新更新