有没有办法让VBA(在Office 2011 for Mac中)运行AppleScript?

时间:2017-09-24 16:04:44

标签: excel vba macos excel-vba applescript

此问题之前已被问过,但建议的答案(“Office 2011 for Mac中的VBA Shell功能”)似乎不起作用。

我的Applescript,Run_Script.scpt,创建一个小的txt文件。它在编辑器中运行完美。当编译为应用程序并在桌面上双击时,它也可以完美运行。

我需要从VBA程序调用它。我试过这些:

Sub sub_scripttest()

Dim i As Integer
Dim result_long As Long
Dim result_str As String

'//==  the original question was this:
result_str = Shell("Macintosh HD:Applications:Run_Script.app", vbNormalFocus)
result_str = Shell("Macintosh HD/Applications/Run_Script.app", vbNormalFocus)
'//== gives a modal dialog box:  "Runtime error 53: File not found"

'//==  the suggested solution was this:
result_str = MacScript("do shell script ""Macintosh HD:Applications:Run_Script.app""")
result_str = MacScript("do shell script ""Macintosh HD/Applications/Run_Script.app""")
'//== gives a modal dialog box: "Runtime error 5: Invalid Procedure call or Argument"

'//==  another suggested solution was this:
result_long = system("Macintosh HD:Applications:Run_Script.app")
result_long = system("Macintosh HD/Applications/Run_Script.app")
'//==  appears to run, but returns   result_long=32512,   which is not a successful termination code.
'//==  also the desktop shows that the script did not run. The txt file was not created.

'//==  I even tried:
result_str = AppleScriptTask("Run_Script.scpt", "my_command", "")
'//==  and got: "Compile Error: Sub or Function not defined."

End Sub

我有OS X El Capitan 10.11.6。我正在运行Office 2011 for Mac。 Excel已更新至14.7.2

我做错了什么?

1 个答案:

答案 0 :(得分:1)

在ElCapitain上使用Excel 2011(14.0.0),以下2个宏正在运行。 您必须调整驱动器/用户/脚本文件的路径。

在VBA宏中调用脚本:

s = "run script (""ElCapitain:Users:imac27:Desktop:Test_Script.scpt"" as alias)"
Temp = MacScript(s)

在VBA宏中调用应用程序:

s = "run script (""ElCapitain:Users:imac27:Desktop:Test_App.app"" as alias)"
Temp = MacScript(s)