How To Open Non-Excel File with Excel Mac VBA

ⅰ亾dé卋堺 提交于 2019-12-13 05:08:32

问题


There are numerous Q&A's for Excel WINDOWS, but none for Excel MAC that specifically answer this question.

Using Excel Mac 2011 VBA, how do I open a non-Excel file using the file's default app?

After much research and testing, I figured out the answer. See below.


回答1:


This works running Microsoft Excel 14.7.2 (14.7.2) on macOS 10.12.6.

```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

```

For Excel Windows, see
How can Excel Windows VBA open file using default application



来源:https://stackoverflow.com/questions/50690783/how-to-open-non-excel-file-with-excel-mac-vba

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