Mac版Office 2011中的VBA Shell功能 [英] VBA Shell function in Office 2011 for Mac
问题描述
我正在尝试从Word 2011 for Mac中的VBA宏启动外壳脚本,该脚本将在终端"窗口中运行.我已经尝试过同时使用Shell函数和 MacScript
函数,但是无论哪种情况,VBA解释器似乎都无法找到该脚本.
I am trying to launch a shell script from a VBA macro in Word 2011 for Mac that will run in a Terminal window. I have tried using both the Shell function and the MacScript
function, but the VBA interpreter doesn't seem to be able to find the script in either case.
根据VBA参考文档,以下方法应该起作用:
According to the VBA reference documentation, the following should work:
RetVal = Shell("Macintosh HD:Applications:Calculator.app", vbNormalFocus)
这会产生运行时错误53找不到文件".
This produces a run-time error 53 'File not found'.
有什么建议吗?
推荐答案
Mac上的Shell()
VBA函数似乎要求完整路径为HFS样式的路径(用冒号而不是斜杠).它似乎也没有像Windows那样接受参数(如果添加了任何参数,则会报告找不到路径"错误).
The Shell()
VBA function on Mac appears to require the full path as an HFS-style path (with colons instead of slashes). It also doesn't appear to accept arguments as it does on Windows (reporting a 'Path not found' error if any arguments are added).
还可以使用MacScript()
VBA功能:MacScript("do shell script ""command""")
.这可能是最简单的选择,也是我建议做的事情.缺点是开销很大(每次通话100-200ms).
The MacScript()
VBA function can also be used: MacScript("do shell script ""command""")
. This is likely to be the simplest option and what I would suggest doing. The downside is that it has quite a lot of overhead (100-200ms per call).
另一种替代方法是标准C库中的system()
函数:
Another alternative is the system()
function from the standard C library:
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()
.
system()
only returns the exit code. If you want to get the output from the command, you could use 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,则必须对其进行更改.
Note that several of the Long
arguments in the above example are pointers, so will have to be changed if a 64bit version of Mac Word is ever released.
这篇关于Mac版Office 2011中的VBA Shell功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!