Office 2011 for Mac中的VBA Shellfunction

我正尝试从Mac版Word 2011中的VBAmacros启动一个shell脚本,该脚本将在terminal窗口中运行。 我已经尝试使用Shell函数和MacScript函数,但是VBA解释器似乎无法在任何情况下find脚本。

根据VBA参考文档,以下应该工作:

  RetVal = Shell("Macintosh HD:Applications:Calculator.app", vbNormalFocus) 

这会产生运行时错误53'未find文件'。

有什么build议么?

Mac上的Shell() VBA函数似乎需要将完整path作为HFS样式的path(冒号代替斜杠)。 它也没有像在Windows上那样接受参数(如果添加了任何参数,报告“path未find”错误)。

MacScript() VBA函数也可以使用: MacScript("do shell script ""command""") 。 这可能是最简单的select,我会build议做的。 缺点是它有相当多的开销(每个电话100-200ms)。

另一个select是标准C库中的system()函数:

 Private Declare Function system Lib "libc.dylib" (ByVal command As String) As Long Sub RunSafari() Dim result As Long result = system("open -a Safari --args http://www.google.com") Debug.Print Str(result) End Sub 

有关文档,请参阅http://pubs.opengroup.org/onlinepubs/009604499/functions/system.html

system()只返回退出代码。 如果你想从命令中获得输出,你可以使用popen()

 Private Declare Function popen Lib "libc.dylib" (ByVal command As String, ByVal mode As String) As Long Private Declare Function pclose Lib "libc.dylib" (ByVal file As Long) As Long Private Declare Function fread Lib "libc.dylib" (ByVal outStr As String, ByVal size As Long, ByVal items As Long, ByVal stream As Long) As Long Private Declare Function feof Lib "libc.dylib" (ByVal file As Long) As Long Function execShell(command As String, Optional ByRef exitCode As Long) As String Dim file As Long file = popen(command, "r") If file = 0 Then Exit Function End If While feof(file) = 0 Dim chunk As String Dim read As Long chunk = Space(50) read = fread(chunk, 1, Len(chunk) - 1, file) If read > 0 Then chunk = Left$(chunk, read) execShell = execShell & chunk End If Wend exitCode = pclose(file) End Function Sub RunTest() Dim result As String Dim exitCode As Long result = execShell("echo Hello World", exitCode) Debug.Print "Result: """ & result & """" Debug.Print "Exit Code: " & str(exitCode) End Sub 

请注意,以上示例中的几个Long参数都是指针,因此如果发布了64位版本的Mac Word,将不得不更改这些指针。

希望你已经find了答案,但你只需要完整的path:

RetVal = Shell("Macintosh HD:Applications:Calculator.app:" & _ "Contents:MacOS:Calculator", vbNormalFocus)

另一个例子是这样的:

RetVal = Shell("Macintosh HD:Users:brownj:Documents:" & _ "rnaseq:IGV_2.0.14:igv.sh", vbNormalFocus)

另一个问题是这样的:权限会导致您的脚本由于AppleScript环境和用户的bash环境之间的差异而失败。 这个问答帮我弄明白了。 为了让我的脚本正常工作,我必须解决一些path和权限问题(不是脚本本身,而是脚本触及的东西)。

这里是我的build议,希望能在您的疑难解答中提供更好的洞察力,而不是我在使用AppleScript Editor之前看到的无意义的Excel错误:

  1. 使用AppleScript编辑器来确认脚本实际上是以任何用户和任何环境variables恰好被使用的方式工作的:

    1. 在Spotlight中,开始input“applescript editor”直到出现,然后点击它
    2. 创build一个新的AppleScript编辑器文件
    3. input你的简单的脚本到新文件中, 而不用加倍双引号 – 我的读取

       do shell script "parseCsvAndOpen.sh" 
    4. 点击脚本的“运行”button
    5. 跟踪任何问题,进行更改,然后重复点击“运行”button,直到从AppleScript编辑器中执行它为止
      • 这里的好消息是,如果您需要返回到StackOverflow或Google寻求帮助,则您的search范围更窄;-)
  2. 现在将您的简单脚本从AppleScript编辑器复制到您的vba并确认它仍然有效

    1. 我能够加倍双引号,并把它放在MacScript代码后面的双引号中:

       MacScript "do shell script ""parseCsvAndOpen.sh""" 

      那确实是一个,两个,然后是三个双引号字符! (大概是逃避双引号)

只是没有足够的评论点,但感觉这是罗宾骑士答案的补充。 学分当然还是给他的。 对于Excel 15.18(2015),标准C库中system()函数中使用的公开调用不再需要–args关键字:

 Private Declare Function system Lib "libc.dylib" (ByVal command As String) As Long Sub RunSafari() Dim result As Long result = system("open -a Safari http://www.google.com") Debug.Print Str(result) End Sub 

这工作正常。 2011年没有testing这个。