如何使用Excel Mac VBA打开非Excel文件 [英] How To Open Non-Excel File with Excel Mac VBA

查看:351
本文介绍了如何使用Excel Mac VBA打开非Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Excel WINDOWS有很多问答,但是对于Excel MAC没有专门回答此问题的问题.

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

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

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.

推荐答案

这可以在macOS 10.12.6上运行Microsoft Excel 14.7.2(14.7.2).

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

```

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

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

这篇关于如何使用Excel Mac VBA打开非Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆