【发布时间】:2019-04-22 17:51:08
【问题描述】:
我正在 Excel for Mac(修订版 16.19)上开发 VBA 程序。因为我需要在(笔记本电脑)屏幕上留出更多空间来显示结果,所以我想在打开工作簿时隐藏功能区。
到目前为止,我看到的所有解决方案都只能在 Windows 上运行,而不能在 Mac 上运行。我还尝试通过 Applescript 来使用 Macscript(见下文)。如果我从 scripteditor 运行此脚本,但未嵌入 VBA,则该脚本可以正常工作。
tell application "System Events" to tell process "Microsoft Excel"
set frontmost to true
keystroke "r" using {command down, option down}
end tell
在 VBA 中是这样的:
Sub example()
Dim toggleRibbon As String
toggleRibbon = "tell application ""System Events"" to tell process ""Microsoft Excel""" & vbNewLine & _
"set frontmost to true" & vbNewLine & _
"keystroke ""r"" using {command down, option down}" & vbNewLine & _
"end tell"
Debug.Print toggleRibbon 'to check format (use of double quotes, etc.)
MacScript (toggleRibbon)
End Sub
在运行时执行此代码会出现错误 5
谁能解决我的问题?
【问题讨论】:
-
对这个东西不是很熟悉,但是你的命令是Applescript而不是VBA,所以我猜VBA不会理解它。如果您在此处查找包含单词
osascript的答案,您将看到您可以使用osascript运行Applescript。现在,在 VBA 中,您可以调用Shell(),因此您应该能够执行Shell(XXX),其中XXX是osascript命令或包含osascript命令的bash脚本,其中包含您已经使用的 Applescript 内容有。相当相似的例子在这里stackoverflow.com/a/2198403/2836621 -
在 VBA for Mac 中,您可以使用 Macscript("string") 命令运行 Applescript。 “字符串”必须包含 Applescript(包括换行符)。这是我已经尝试过的。
-
也许在您的问题下单击
edit,如果您尝试过,请添加。可能是您没有正确转义双引号 - 因此请将其添加到您的问题中。 -
Tnx Mark,我已经嵌入了我尝试过的 VBA 示例
标签: excel vba macos applescript